# Force and rotation

I'm making a flying script, but I'm quite new to Unity.

``````function Update () {
if( Input.GetButtonDown( "Horizontal" ) ){
}
if( Input.GetButtonDown( "Vertical" ) ){
}
}

``````

Ok, first, I need to replace "rigidbody.AddForce" with something that makes the object go forward relative to its own direction, so it goes forward in the direction its facing.

Also, for the "AddTorque" part, how would I make it so that it only right when I press right, left when I press left, and so on. Currently, it can only rotate down and left, even when I press the opposite button.

I hope I'm not asking too much. ~Max

If you change AddForce(), to AddRelativeForce() it will apply the force on the axis relative to the objects coordinate system.

As for your Torque question, if you use Input.GetAxis("Vertical") you will get a value in the range -1..1. So -1 for left and 1 for right, 0 for no change.

You may also want to look at calling these functions in the FixedUpate() instead of Update().

``````var maxTorque : float = 1.0f;
var forwardForce : float = 10.0f;

var currentTorqueHor : float = 0.0f;
var currentTorqueVer : float = 0.0f;

function Update()
{
currentTorqueHor = maxTorque * Input.GetAxis("Horizontal");
currentTorqueVer = maxTorque * Input.GetAxis("Vertical");
}

function FixedUpdate()
{
}

``````

Just thought I'd add a note quickly to explain why I mention AddRelativeForce() at the start of the post and use AddForce() in my script.

transform.forward returns the forward vector of the transform in world space, applying that vector relative to your objects rotating local coordinate system is going to yield some strange results.

You could use AddRelativeForce() if you used Vector3.forward to get the forward vector, as forward is forward to your object in it's local space no matter what. =P

I have a question about Force and Rotation, so thought I would post it here.

I am using AddRelativeTorque to rotate a rigidbody around its own z-axis. The problem is it seems to rotate around the world axis instead. Here is my code, can someone see what I'm doing wrong here?

Roll =(Input.GetAxis("Roll"));

if(Roll != 0) { rigidbody.AddRelativeTorque (0,0, -1*Roll*RollR); }

Thanks