# The camera goes up and down when following a rolling sphere

I have this CameraController script attached to my camera, to follow a sphere that moves using WSAD.

public class CameraController : MonoBehaviour
{

public GameObject player;

private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}

// Update is called once per frame
void FixedUpdate()
{
transform.position = player.transform.position + offset;
}
}


It follows the object fine, but it bounces up and down on the Y axis when it's moving, and I don't know how to stop the camera from doing it.

• Have you tried doing this in LateUpdate instead of FixedUpdate? That's the only thing I can think of without more information (such as a video of the camera bouncing, or the code for your character controller). – Alex F Jun 9 at 23:28
• Is your sphere centered around player.transform.position? If it's offset at all from this center, then we'd expect the camera to bob with this code, as the object's position swings around the axis of rotation. – DMGregory Jun 10 at 0:08

## 2 Answers

You could just lock the height of the camera to a value. This should stop bouncing.

public GameObject player;
float height;

private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
height = transform.position.y;
}

// Update is called once per frame
void FixedUpdate()
{
transform.position = player.transform.position + offset;
transform.position.y = height;
}


I hope this might help you

i dont know how your ball is controlled but i assumed it is controlled by rigidBody physics. its obvious that for something that is controlled by physics you have some noisy jittery moves. your code works like attaching or childing camera to ball without deriving rotation. so camera will have the same jittey moves.

best solution is to lerp camera to wanted position instead of stick it to exact position. your update callback will be like this:

float speed=0.1f;
void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position ,player.transform.position + offset,speed);
}