I’m trying to make an airplane/spaceship do a barrel roll, then stop, using AddRelativeTorque (pitch and yaw too, but let’s focus on roll for now). I know I could use angular drag, but I would rather create a script for it. I found a script another poster made, and pulled some bits out that I could use, but I’ve run into a problem.
I can’t work out how to check the angular velocity of the vehicle’s rigidbody, so that the script knows when to apply counter-torque. My current solution (below) gives an error on the “else if” line, and I don’t know how to fix it.
public float pitchFactor;
public float rollFactor;
public float yawFactor;
public Rigidbody controlledShip;
void FixedUpdate ()
{
float horizontal, vertical;
horizontal = Input.GetAxis ("Horizontal");
vertical = Input.GetAxis ("Vertical");
Vector3 localAngleVelo = controlledShip.transform.InverseTransformVector (controlledShip.GetComponent<Rigidbody> ().angularVelocity);
if (horizontal != 0f) {
controlledShip.AddRelativeTorque (Vector3.forward * (horizontal * (rollFactor * Time.deltaTime)));
} else if (localAngleVelo.forward > 0.0) {
controlledShip.AddRelativeTorque (Vector3.forward * (rollFactor * Time.deltaTime));
}
}
Any solutions?