- Home /
Camera follow player when player rotates in y-axis
Hi. I am using the following code in my camera to follow a player object. The camera rotates to the direction the player is moving. The rotation is smooth for all axes except for when the player rotates in the y-axis. I want to have the camera follow animation smooth when the player moves on the y axis as well.
void LateUpdate () {
//transform.position = Vector3.SmoothDamp(transform.position, (target.transform.position + offset), ref velocity, smoothTime);
Vector3 destination = target.transform.rotation * offset;
destination += target.transform.position;
//transform.position = destination;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, smoothTime);
//transform.LookAt(target.transform.position, target.transform.up);
LookAtTarget();
}
void LookAtTarget(){
Vector3 lookPos = target.transform.position - transform.position;
Quaternion qt = Quaternion.LookRotation(lookPos, target.transform.up);
transform.rotation = Quaternion.Lerp(transform.rotation, qt, rotatingSpeed * Time.deltaTime);
//transform.rotation = qt;
}
The below gif will give you a better idea:
Answer by cristianm_unity · Dec 30, 2019 at 10:48 PM
Hi @Veeresh01! I tried your code and for me, the camera rotation is smooth for all the axes. I wonder if the problem is that it is rotating too fast (i.e maybe the rotation in the y axis is bigger, so that is way it does not seem as smooth as the other axes)? Unfortunately, in the gif I cannot appreciate very well the behavior you want to achieve.
Your answer
Follow this Question
Related Questions
Camera shaking on crouching and rotating player 0 Answers
Camera following player gameObject,Camera follows player, with script attached. 1 Answer
Fixed-Position Camera that follows player,Fixed Position Camera that follows target. 2 Answers
Camera rotation 0 Answers
How to look player and enemy at the same time using cinemachine FreeLook camera. 0 Answers