Hello, I made a simple camera controller which follows the player around from an upper perspective, I’m also using Lerp to make the camera less stiff, and I also use the Q and E keys to rotate it around the player object.
I move the player using the NavMeshAgent.destination function.
The problem is that whenever I move the player and rotate the camera at the same time the camera “drifts” and the player moves further away from the camera. This effect is fixed if I remove the Lerp but I want the camera to not be so stiff but also not go further than a fixed distance away from the player, what would be the easiest way to accomplish this?
Here is the CameraController script:
public class CameraController : MonoBehaviour
{
public float rotationSpeed; //this controls the speed of the rotation
public float damping = 1; //this adds "floatiness" to the camera
private Transform player;
private float movement;
private Vector3 offset;
void Start()
{
//Find the Object with the Player tag and set the initial offset
player = GameObject.FindGameObjectWithTag("Player").transform;
if (player == null)
{
Debug.LogError("Could not find GameObject with 'Player' tag");
}
offset = transform.position - player.position;
}
void LateUpdate()
{
Vector3 desiredPosition = player.position + offset;
Vector3 position = Vector3.Lerp(transform.position,
desiredPosition,
Time.deltaTime * damping);
transform.position = position;
//Rotation has Q and E as Keys
float movement = -Input.GetAxis("Rotation") * rotationSpeed * Time.deltaTime;
if (!Mathf.Approximately(movement, 0.0f))
{
transform.RotateAround(player.position, Vector3.up, movement);
offset = transform.position - player.position;
}
transform.LookAt(player);
}
}