0
\$\begingroup\$

i'm using the script down below to make my camera look at mouse x and y position, and it has a limitation for my camera rotation.the problem is the way my camera rotates is not smooth at all.is there any way to make it smoothly look at position on x and y?

    rotationX += Input.GetAxis("Mouse X") * sensitivityX;

    rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);

    rotationY += Input.GetAxis("Mouse Y") * sensitivityY ;

    rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

    transform.localEulerAngles = new Vector3(-rotationY , rotationX, 0);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might want to add an animated gif to show the issue and what you consider "not smooth". \$\endgroup\$
    – Vaillancourt
    Commented Jun 10, 2019 at 18:23

1 Answer 1

0
\$\begingroup\$

You are rotating it on a fixed time step. Unity's Update method (which I assume you are using) uses a variable time step. This means you have two options:

Normally you would perform your physics update in FixedUpdate, but input does not work in FixedUpdate. This means you need to multiply by your time step, AKA the duration of the previous frame. Your new code would look like such:

private void Update()
{
    rotationX += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
    rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
    rotationY += Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
    rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    transform.localEulerAngles = new Vector3(-rotationY , rotationX, 0);
}

With this new code, you will also need to increase your sensitivity variables.

\$\endgroup\$
1
  • \$\begingroup\$ Because Mouse X and Mouse Y are position deltas since the last frame, they're already smaller on short frames and longer on long frames, assuming a constant mouse speed in both cases. So this is one case where it's actually not correct to add an extra * Time.deltaTime as that double-corrects and can introduce unwanted artifacts. Also, it's not true that input doesn't work in FixedUpdate (this is a popular misconception - try it!), we just have to be cautious how we use it, as input is updated once per frame not once per fixed timestep. \$\endgroup\$
    – DMGregory
    Commented Jun 11, 2019 at 4:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .