Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cap Rotation of Helicopter

Discussion in 'Physics' started by Frozen Sinn, May 1, 2015.

  1. Frozen Sinn

    Frozen Sinn

    Joined:
    May 1, 2015
    Posts:
    2
    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<Rigidbody> ();
    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;
    }


    }
     
  2. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    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)
    {
    }
     
  3. Frozen Sinn

    Frozen Sinn

    Joined:
    May 1, 2015
    Posts:
    2
    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...
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    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.
     
  5. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    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.