- Home /
How can I lock my camera on the Y-axis?
Hello! I've added a little hover animation to my player ship, but now - as the camera is tracking my player ship - the camera bobs slightly as it tracks the animation.
Is there a way to lock my camera on the Y-axis as to negate this bob?
Here's my CameraFollow script:
public class CameraFollow : MonoBehaviour {
public Transform target;
public float smoothing = 5f;
Vector3 offset;
void Start()
{
offset = transform.position - target.position;
}
void FixedUpdate ()
{
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
Thanks in advance for any help!
Jamin
Answer by Ekta-Mehta-D · Aug 19, 2015 at 09:17 AM
hmm something like this :
transform.position = Vector3.Lerp (transform.position, new Vector3 (targetCamPos.x , 0.0f , targetCamPos.z) , smoothing * Time.deltaTime);
This may work for you. Thanks.
Hi Ekta - many thanks for the response. This didn't work I'm afraid; caused my camera to zoom quickly towards my player ship, where it stayed beneath the plane of my game world.
(I literally just swapped in your line of code for the one I already had...)
Change line 15 in your code to:
Vector3 targetCamPos = new Vector3(target.position.x + offset.x, transform.position.y, target.position.z + offset.z);
The point is to assemble a new vector 3, and keep the y value constant.
Your answer
Follow this Question
Related Questions
Is there any way to keep game object withing the limit of Camera FOV 3 Answers
Isometric - Camera follow an object when at the edge of the screen 1 Answer
How to create a panning, rotating camera with Cinemachine Freelook camera? 0 Answers
Unity - Dancing Ball World camera following and rotating system 1 Answer
Free Look Camera 1 Answer