- Home /
How to rotate an object along the x axis with mouse input?
I'm trying to make a game object rotate around the x axis when the player moves the mouse up or down.
This is the closest I got.
transform.Rotate(Input.mousePosition);
But the rotation is un-restricted and happens even when I'm not moving the mouse.
Answer by aldonaletto · Aug 02, 2012 at 03:44 AM
You could use Input.GetAxis("Mouse Y"), like in the MouseLook script:
var sensitivity: float = 10; // X angles run upside down: negative is up, positive is down var limUp: float = -70; // max angle up var limDn: float = 50; // max angle down
private var euler: Vector3;
function Start(){ euler = transform.localEulerAngles; // save initial euler angles }
function Update(){ euler.x -= Input.GetAxis("Mouse Y") * sensitivity; euler.x = Mathf.Clamp(euler.x, limUp, limDn); transform.localEulerAngles = euler; }
Awesome, thanks! It works just have to tinker with the variables a little bit.