0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Alex F Jun 9 at 23:28
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jun 10 at 0:08
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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);
    } 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.