Search Unity

making a helicopter from tutorial

Discussion in 'Getting Started' started by bobthenob, Feb 19, 2019.

  1. bobthenob

    bobthenob

    Joined:
    Sep 2, 2013
    Posts:
    26
    hi im fairly new to unity but am getting better.
    I'm trying to make a helicopter I have a simple helicopter I made in 3ds max
    that I exported with the correct axsis.
    I found this tutorial that is java script ive manged to convert to c#.
    link to Tutorial

    https://andrewgotow.files.wordpress.com/2014/08/helicoptertutorial.pdf

    But it dosnt say how the helicopter is linked, so dont know if that is what im doing wrong.
    I have a empty game object with the script and rigidbody on, that is the parent of the body and rota's
    the rota's rotate with the right joystick going left slows it or right speeds it up on my ps4
    and the whole think rotates left and right with the left joystick on ps4
    so that seems good.
    but i cant get it to fly it just tumble over sideways
    any help would be great thanks for looking
    here is my project

    https://www.dropbox.com/s/vspdqds99mho0ye/heli2 - Copy.zip?dl=0

    here is the script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class heli : MonoBehaviour
    5. {
    6.      public GameObject main_Rotor_GameObject;            // gameObject to be animated
    7.      public GameObject tail_Rotor_GameObject;          // gameObject to be animated
    8.     public float max_Rotor_Force  = 22241.1081f;    // newtons
    9.     public float max_Rotor_Velocity = 7200;         // degrees per second
    10.     public float rotor_Velocity  = 0.0f;            // value between 0 and 1
    11.     public float rotor_Rotation = 0.0f;             // degrees... used for animating rotors
    12.     public float max_tail_Rotor_Force  = 15000.0f;      // newtons
    13.     public float max_Tail_Rotor_Velocity = 2200.0f;      // degrees per second
    14.     public float tail_Rotor_Velocity  = 0.0f;           // value between 0 and 1
    15.     public float tail_Rotor_Rotation  = 0.0f;           // degrees... used for animating rotors
    16.     public float forward_Rotor_Torque_Multiplier = 0.5f;            // multiplier for control input
    17.     public float sideways_Rotor_Torque_Multiplier =  0.5f;          // multiplier for control input
    18.     public bool main_Rotor_Active = true;        // boolean for determining if a prop is active
    19.     public bool tail_Rotor_Active = true;        // boolean for determining if a prop is active
    20.     public Vector3 torqueValue;
    21.     public Rigidbody rb;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.     }
    26.    //Forces are applied in a fixed update function so that they are consistent no matter what the frame rate of the game is.This is
    27.     // important to keeping the helicopter stable in the air. If the forces were applied at an inconsistent rate, the helicopter would behave
    28.     // irregularly.
    29.     void FixedUpdate()
    30.     {
    31.         // First we must compute the torque values that are applied to the helicopter by the propellers. The "Control Torque" is used to simulate
    32.         // the variable angle of the blades on a helicopter and the "Torque Value" is the final sum of the torque from the engine attached to the
    33.         // main rotor, and the torque applied by the tail rotor.
    34.        // Vector3 torqueValue;
    35.         Vector3 controlTorque =  new Vector3(Input.GetAxis("Vertical") * forward_Rotor_Torque_Multiplier, 1.0f, -Input.GetAxis("Horizontal2") * sideways_Rotor_Torque_Multiplier);
    36.         // Now check if the main rotor is active, if it is, then add it's torque to the "Torque Value", and apply the forces to the body of the
    37.         // helicopter.
    38.         if (main_Rotor_Active == true)
    39.         {
    40.             torqueValue += (controlTorque * max_Rotor_Force * rotor_Velocity);
    41.             // Now the force of the prop is applied. The main rotor applies a force direclty related to the maximum force of the prop and the
    42.             // prop velocity (a value from 0 to 1)
    43.            // rb.AddRelativeForce(Vector3.up * max_Rotor_Force * rotor_Velocity);
    44.             // This is simple code to help stabilize the helicopter. It essentially pulls the body back towards neutral when it is at an angle to
    45.             // prevent it from tumbling in the air.
    46.             if (Vector3.Angle(Vector3.up, transform.up) < 80)
    47.             {
    48.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0), Time.deltaTime * rotor_Velocity * 2);
    49.             }
    50.         }
    51.         // Now we check to make sure the tail rotor is active, if it is, we add it's force to the "Torque Value"
    52.         if (tail_Rotor_Active == true)
    53.         {
    54.             torqueValue -= (Vector3.up * max_tail_Rotor_Force * tail_Rotor_Velocity);
    55.         }
    56.         // And finally, apply the torques to the body of the helicopter.
    57.        rb.AddRelativeTorque(torqueValue);
    58.    
    59.     }
    60.     void Update()
    61.     {
    62.         // This line simply changes the pitch of the attached audio emitter to match the speed of the main rotor.
    63.        // GetComponent.< AudioSource > ().pitch = rotor_Velocity;
    64.         // Now we animate the rotors, simply by setting their rotation to an increasing value multiplied by the helicopter body's rotation.
    65.         if (main_Rotor_Active == true)
    66.         {
    67.             main_Rotor_GameObject.transform.rotation = transform.rotation * Quaternion.Euler(0, rotor_Rotation, 0);
    68.         }
    69.         if (tail_Rotor_Active == true)
    70.         {
    71.             tail_Rotor_GameObject.transform.rotation = transform.rotation * Quaternion.Euler(tail_Rotor_Rotation, 0, 0);
    72.         }
    73.         // this just increases the rotation value for the animation of the rotors.
    74.         rotor_Rotation += max_Rotor_Velocity * rotor_Velocity * Time.deltaTime;
    75.         tail_Rotor_Rotation += max_Tail_Rotor_Velocity * rotor_Velocity * Time.deltaTime;
    76.         // here we find the velocity required to keep the helicopter level. With the rotors at this speed, all forces on the helicopter cancel
    77.         // each other out and it should hover as-is.
    78.         var hover_Rotor_Velocity = (rb.mass * Mathf.Abs(Physics.gravity.y) / max_Rotor_Force);
    79.         var hover_Tail_Rotor_Velocity = (max_Rotor_Force * rotor_Velocity) / max_tail_Rotor_Force;
    80.         // Now check if the player is applying any throttle control input, if they are, then increase or decrease the prop velocity, otherwise,
    81.         // slowly LERP the rotor speed to the neutral speed. The tail rotor velocity is set to the neutral speed plus the player horizontal input.
    82.         // Because the torque applied by the main rotor is directly proportional to the velocity of the main rotor and the velocity of the tail rotor,
    83.         // so when the tail rotor velocity decreases, the body of the helicopter rotates.
    84.         if (Input.GetAxis("Vertical2") != 0.0)
    85.         {
    86.             rotor_Velocity += Input.GetAxis("Vertical2") * 0.001f;
    87.         }
    88.         else
    89.         {
    90.             rotor_Velocity = Mathf.Lerp(rotor_Velocity, hover_Rotor_Velocity, Time.deltaTime * Time.deltaTime * 5);
    91.         }
    92.         tail_Rotor_Velocity = hover_Tail_Rotor_Velocity - Input.GetAxis("Horizontal");
    93.         // now we set velocity limits. The multiplier for rotor velocity is fixed to a range between 0 and 1. You can limit the tail rotor velocity
    94.         // too, but this makes it more difficult to balance the helicopter variables so that the helicopter will fly well.
    95.         if (rotor_Velocity > 1.0)
    96.         {
    97.             rotor_Velocity = 1.0f;
    98.         }
    99.         else if (rotor_Velocity < 0.0)
    100.         {
    101.             rotor_Velocity = 0.0f;
    102.         }
    103.     }
    104. }