- Home /
Rotating Object with Mouse Axis is very choppy
I am trying to create this very simple controller for my character to move using velocity and to rotate the camera and character with the mouse. I am making a FPS and am looking for a feel similar to COD as far as character controls go. My problem thus far is that the rotation of the character and camera are very choppy and jumpy. It appears that it makes minor movements and jumps straight back to the original position. I realize that it may be very simple, but i have been struggling with solving it for the last few days. Any tips are greatly appreciated! rotate the camera
public class PlayerControls : MonoBehaviour {
public float moveSpeed, Sensitivity;
void Start(){
}
// Update is called once per frame
void Update () {
rigidbody.transform.rotation = Quaternion.Euler(-Input.GetAxis ("Mouse Y") * Sensitivity, -Input.GetAxis ("Mouse X") * Sensitivity, 0);
rigidbody.velocity = new Vector3(Input.GetAxis ("Horizontal") * moveSpeed,0,Input.GetAxis ("Vertical") * moveSpeed);
}
}
Answer by robertbu · Mar 18, 2014 at 06:14 AM
I'm not sure of your ideal rotation, but replace your Update() with the following for a smoother rotation:
void FixedUpdate () {
euler.x += Input.GetAxis ("Mouse Y") * Sensitivity;
euler.y -= Input.GetAxis ("Mouse X") * Sensitivity;
rigidbody.transform.rotation = Quaternion.Euler(euler);
rigidbody.velocity = new Vector3(Input.GetAxis ("Horizontal") * moveSpeed,0,Input.GetAxis ("Vertical") * moveSpeed);
}
That worked great thanks! Do you happen to know how i would make it so the up/down rotation won't spin all the way around? That way it will be more like the player looking up and down and not actually doing flips.
I am also having trouble aligning the velocity with the object rotation. Right now it is moving in the same direction regardless what direction the object is facing. Any suggestions?
To limit the rotation, just clamp the values in 'euler' before assigning them the rotation. Example:
euler.x = $$anonymous$$athf.Clamp(euler.x, -90, 90);
As for the movement direction, your current code does not take into account any form of direction. Try changing line 5 to:
rigidbody.velocity = transform.forward * Input.GetAxis ("Vertical") * moveSpeed);
Thanks a ton! Im pretty new to this and you've been a great help!
I have one more problem! i think im missing something obvious but i can't move forward or back while right and left work perfectly, and my rotation still goes all the way around
public class PlayerControls : $$anonymous$$onoBehaviour { public float moveSpeed, Sensitivity; Vector3 euler; void Start(){ } // Update is called once per frame void FixedUpdate () { euler.y = $$anonymous$$athf.Clamp (euler.y, -90,90); euler.x -= Input.GetAxis ("$$anonymous$$ouse Y"); euler.y += Input.GetAxis ("$$anonymous$$ouse X"); rigidbody.transform.rotation = Quaternion.Euler(euler * Sensitivity); rigidbody.velocity = transform.forward * Input.GetAxis ("Vertical") * moveSpeed; rigidbody.velocity = transform.right * Input.GetAxis ("Horizontal") * moveSpeed; } }