# Moving an object based on its angle

I’m trying to get an object to move according to its X Y angle (ie if its flat, it moves upwards; tilts forwards, it moves forwards etc - basically 90 degrees from its current angles) :

``````function Update () {
var bank=0.0;
var tilt=0.0;
var thrust=0.0;

bank=Input.GetAxis("Mouse X")*Time.deltaTime*15.0;
tilt=Input.GetAxis("Mouse Y")*Time.deltaTime*15.0;

if (Input.GetButton("Fire1"))
{
thrust+=Time.deltaTime*0.05;
}
else
{
thrust=0.0; //-=Time.deltaTime;
}

transform.Rotate(bank,0.0,tilt);

x=transform.eulerAngles.x;
y=transform.eulerAngles.y;
z=0.0;

transform.Translate(Vector3(x,y,z)*Time.deltaTime*thrust*10.0);

Debug.Log("X Angle:"+x+" Y Angle :"+y+" Z Angle :"+z);
}
``````

Unfortunately, at the moment its not working correctly (especially for all angles) - tilting and banking is fine, but movement isn’t always in the right direction. The problem is calculating the 90 degree angle (and then using that as a basis of movement). Can anyone help ?

Nicholas

I guess you’re trying to make helicopter-style controls? Or at least that’s what it sounds like.

Instead of trying to rotate the vector, you want to flatten it. Use transform.up to get the upwards direction; save that to a variable, and then set variable.y to 0.

There’s also a helicopter script from OTEE around somewhere that uses the physics engine, if that is what you’re trying to accomplish.

Its actually more like the controls in Zarch (aka Virus), but using .up was the way to go.

Thanks