# Tilting an Object Without Spinning It

I am trying to write a script that will rotate an object on the x and z axes without spinning it on the y axis. As I have it now it more or less works but as I move it around it gradually spins around so that the object has visibly span around from it’s starting orientation.

I’ve tried countless variations of Transform.Rotate, local rotation, eulerAngle, using Mathf.Clamp to restrict the values the y rotation can take etc. but nothing I try is working out. Surely this is a relatively simply procedure? This is what I’ve got at the moment.

`public var speed : float = 60;

function Update () {

var yMove : float = Input.GetAxis(“Horizontal”) * Time.deltaTime * speed; //read input from the “Horizontal” axes
var xMove : float = Input.GetAxis(“Vertical”) * Time.deltaTime * speed; //read input from the “Vertical” axes

transform.Rotate(-xMove, yMove, 0);

}`

Thanks in advance for any help offered.

var rotationLimitX : float;
var rotationLimitY : float;
function FixedUpdate ()
{

``````    var yMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * speed; //read input from the "Horizontal" axes
var xMove : float = Input.GetAxis("Vertical") * Time.deltaTime * speed; //read input from the "Vertical" axes
if(((xMove<0&&transform.localRotation.x+xMove<rotationLimitX)||(xMove>0&&transform.localRotation.x-xMove>-rotationLimitX))&&((yMove>0&&transform.localRotation.y+xMove<rotationLimitY)||(yMove<0&&transform.localRotation.y-xMove>-rotationLimitY)))
{
transform.Rotate(-xMove, yMove, 0);
}
else
{
if(xMove<0)
{
transform.localRotation.x = rotationLimitX;
}
else
{
transform.localRotation.x = -rotationLimitX;
}
if(yMove>0)
{
transform.localRotation.y = rotationLimitY;
}
else
{
transform.localRotation.y = -rotationLimitY;
}
}
}
``````

Had you tried something like this??

You should really be using fixedupdate for this, since it’s controlling movement of a game object.