- Home /
How do we write a C# script to smoothly rotate objects based on Input.GetAxis("Mouse *")?
I wrote this script on my own and it works fine. However, it cannot rotate objects smoothly. How can make it so it can rotate the object and its parent smoothly?
I use this script to rotate my camera vertically and its parent horizontally:
public class MyCamera : MonoBehaviour {
public float rotationSpeed = 100;
public Transform parent;
void Update() {
transform.localEulerAngles += new Vector3(Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime, 0, 0);
parent.transform.localEulerAngles += new Vector3(0, Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime. 0);
}
}
Comment
What exactly is "not smoothly" about it? I've used such code many times and it's as smooth as I'd want it to be. Could it be that your framerate is too low?
BTW, rotating the parent also rotates the child, so you might want to remove that first rotation command and only rotate the parent.
Best Answer
Answer by Pecek · Oct 13, 2013 at 07:08 AM
Read the docs, euler angles/ local euler angles isn't suitable for the job. Use Transform.Rotate instead. Everything else looks fine for me.