Здравствуйте, у меня возникла проблема. Написал скрипт для камеры он работает но камера выходит за пределы сцены, то есть показывает часть фона, как выставить границы камеры не выходящие за пределы уровня?
Код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float damping = 1.5f;
public Vector2 offset = new Vector2(2f, 1f);
public bool faceLeft;
private Transform player;
private int lastX;
void Start()
{
offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer(faceLeft);
}
public void FindPlayer(bool playerFaceLeft)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
lastX = Mathf.RoundToInt(player.position.x);
if (playerFaceLeft)
{
transform.position = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
}
void Update()
{
if (player)
{
int currentX = Mathf.RoundToInt(player.position.x);
if (currentX > lastX) faceLeft = false; else if (currentX < lastX) faceLeft = true;
lastX = Mathf.RoundToInt(player.position.x);
Vector3 target;
if (faceLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, damping * Time.deltaTime);
transform.position = currentPosition;
}
}
}