Rotate/Roll

I need my ship to roll left when I hit the left arrow key and roll right when I hit the right arrow key. How should I do that?

(I’ve searched Unity Answers and all the scripts didn’t work. I tried Unity Forums, no help there. Even Google didn’t help me, so unless you’re absolutely sure it will work, don’t direct me to another page.)

transform.RotateAroundLocal(transform.forward,5);

where 5 is the amount of degrees to rotate along the transform.forward axis

For a more advanced solution, access the unity reference for transform

Ok I modified my moving script.

var speed = 50.0;

function Update ()
{   
	if(Input.GetKey("right"))
	{
    transform.RotateAround(transform.forward,0.01);
	}
	
	if(Input.GetKey("left"))
	{
	transform.RotateAround(transform.forward,-0.01);
	}

	var z = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(0, 0, z);
}

The problem here is the line:

transform.RotateAround(transform.forward,0.01);

If I change the transform.forward into a transform.up, the ship rotates around the y axis, but I need it to rotate on the z axis, and transform.forward isn’t giving me that. How should I fix it?

Now that you’ve attached it to the parent gameobject, it should be rotating around

transform.forward

I think this might help you out. Keep in mind this type of movement will not allow colliders to work well. To use physics and colliders and such you’ll need to use rigidbody.AddRelativeForce & rigidbody.AddRelativeTorque.

var rightSpeed = 0.05;
var leftSpeed = -0.05; /// Same as rightSpeed but a negative

function Update ()
{

if(Input.GetKey("right"))
{
transform.Rotate(Vector3.forward, rightSpeed);
}

if(Input.GetKey("left"))
{
transform.Rotate(Vector3.forward, leftSpeed);
}

}

If i remember right you use the axis with which to rotate around. The Z axis is forward so you want to rotate around IT which results in the type of banking/rolling movement you are seeking. Just make sure this script is attached to the GameObject you want to rotate. NOT a parent or child. It must be directly attached to what you want to spin. So if it’s an airplane, this must be attached to the airplane gameobject which has the mesh filter as a component. I sometimes use an empty GameObject to contain a ‘child’ GO which I instantiate. In these cases, the scripts should be attached to the object which you want manipulated and not the empty GameObject holder.