0
\$\begingroup\$

I have a problem, my camera is parented to the FPS Controller prefab, then there is a script I wrote for the rotation of the player horizontally by mouse movement, it all works but the rotation is very choppy and not smooth. How can I have the camera rotating smoothly or should I not parent my camera to the FPS Controller at all and make the rotation through scripting if so then how?

here's the script of character rotation:

void Update () {
    float rotationY = Input.GetAxis ("Mouse X") * 50f;
    transform.Rotate (0, rotationY, 0);


}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Try this:

using UnityEngine;
using System.Collections;

public class RotateTheCamera : MonoBehaviour {

    private float targetAngle = 0;
    private float currentAngle = 0;

    void Update () {

        float mouseXChange = Input.GetAxis("Mouse X");

        targetAngle += mouseXChange * 10;

        if (targetAngle < currentAngle)
        {
            while (currentAngle - targetAngle > 360) targetAngle += 360;
        }
        else if (targetAngle > currentAngle)
        {
            while (targetAngle - currentAngle > 360) targetAngle -= 360;
        }

        currentAngle += Time.deltaTime * (targetAngle - currentAngle);

        if (Mathf.Abs(currentAngle - targetAngle) < 1) targetAngle = currentAngle;

        transform.rotation = Quaternion.Euler(0, currentAngle, 0);
    }
}

I changed the multiplier to 10 here, because 50 seemed too fast, but you could make it 50 still if you wanted.

Hope this helps.

\$\endgroup\$
3
  • \$\begingroup\$ The rotation is quite smooth but after I stop the mouse it keeps rotating which isn't good for a first person shooter, also after a while the rotation slows down and then speeds up again. \$\endgroup\$
    – John Smith
    Aug 3, 2015 at 18:49
  • \$\begingroup\$ Ah, sorry, didn't catch the FPS part. Are you also going to want up and down rotation? \$\endgroup\$ Aug 3, 2015 at 18:53
  • \$\begingroup\$ Yes I need the up and down rotation as well \$\endgroup\$
    – John Smith
    Aug 3, 2015 at 18:56

You must log in to answer this question.

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