I’m trying to get my camera to follow the player and rotate with it. The camera does follow and rotate with the player, but as the player continues to move forward the camera’s y-position is slowly decreasing until it reaches the ground. The camera will then be stuck in that position.
private Transform target;
void Start()
{
target = GameObject.FindWithTag("Player").transform;
}
void LateUpdate()
{
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X") * 4.0f);
transform.LookAt(target);
}
Sorry, if I didn’t adequately explain the problem or my question is a bit confusing to understand. I also apologize if my code is a bit…unorthodox.
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
You are using Lerp in a very wrong way!
First: target.position + (transform.position - target.position).normalized * 10
It says “Pick my target.position and add 10 times the value of the direction between the transform and the target.” So as the target’s trasform is at the ground, and the camera ia above it, it will keep re-calculating it’s way all to the ground.
Second: Time.deltaTime * 5
Yeah, it is the Unity’s sample about how to use the Lerp, but it is wrong, Lerp isn’t meant to be used with Time.deltaTime.
As others said, use Vector3.MoveTowards(transform.position, target.position, maxDistanceDelta);
You Should use vector3.movetowards instead of vector3.lerp.
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
Here what you are trying to do is Lerping position of the the camera from its own position to the target’s position. So for the player, the pivot is going to be at the bottom which in turn results makes the camera lerp to the ground.
Instead of using the script, what you can do is, you can directly attach the camera to the player by making the camera child of the player.
u have to fix the distance between the camera and the player.
something like the camera script here:
yourCameraTutorial
Its a Tutorial which explaines how to make exactly a script how you like it ( at least i gess its what you want). There is also already the code there just to copy-paste.