# Cap Rotation of Helicopter

I am making a game which the player it is a Helicopter which moves in the vertical plane x, y (not horizontal). I have made the basic movement but when my Helicopter goes forward I want it to rotate about 15º but no more and when no forward arrow pressed it has to return to 0º. I’m planning to use an AddTorque but how can I cap de rotation to those angles? For a reference it’s the same move like in Choplifter HD.

Code with the base movement:

using UnityEngine;
using System.Collections;

public class ShipMove : MonoBehaviour {
public float speedx;
public float speedy;
public float speedr;
private Rigidbody box;
public float velx;
public float vely;
public float velz;
// Use this for initialization
void Start () {
box = GetComponent ();
speedx = 90f;
speedy = 20f;
speedr = 10f;
}

// Update is called once per frame
void FixedUpdate () {
float movx = Input.GetAxis (“Horizontal”);
box.AddForce(Vector3.right * movx * speedx, ForceMode.Acceleration);
float movy = Input.GetAxis (“Vertical”);
box.AddForce(Vector3.up * movy * speedy, ForceMode.Acceleration);
box.AddTorque(Vector3.back * movx * speedr, ForceMode.Acceleration);
velx = box.velocity.x;
vely = box.velocity.y;
velz = box.velocity.z;
}

}

You can try putting the AddTorque function inside an if statement that checks if the rotation you want is not exceeding 15 degress:

if (transform.localEulerAngles.x < 15)
{
}

But that wouldn’t stop the rotation it will keep the remaining movement from the torque applied previously. And if at 15º I stop the velocity it wouldn’t be fluid…

I would use MoveRotation rather than torque, otherwise a combination of not adding force unless below the value and adding negative torque when the value is reached is your only solution.

Hmmm, from your code, I don’t think you are modifying the velocity yet, so it is not really accurate to say you are stopping the velocity.

Did the heli really show non-fluid stop? When you stop providing the torque or force, it’s like lifting your foot from the pedal.

Try using rigidbody.MoveRotation. Your animation will look fluid.