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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Base Helicopter Controller

Discussion in 'Assets and Asset Store' started by suncubestudio, Dec 17, 2019.

  1. suncubestudio

    suncubestudio

    Joined:
    Jul 30, 2015
    Posts:
    1
  2. TokyoWarfareProject

    TokyoWarfareProject

    Joined:
    Jun 20, 2018
    Posts:
    787
    This is no doubt best non sim helicopter controller on the store. Yet, I found it had several limitaitons.

    I reworked the code here is my version (modified file attached)


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class HelicopterController : MonoBehaviour
    5. {
    6.     public AudioSource HelicopterSound;
    7.     public Rigidbody Heli_rb;
    8.     public HeliRotorController MainRotorController;
    9.     public HeliRotorController SubRotorController;
    10.  
    11.     public float TurnForce = 3f;
    12.     public float ForwardForce = 10f;
    13.     public float maxForwardTiltForce = 20f;
    14.     private float currentForwardTiltForce;
    15.     float forwardInputValue;
    16.     public float TurnTiltForce = 30f;
    17.  
    18.     public float MaxHeight = 100f;
    19.     private float targetheight ; // altura en la cual estabilizarse
    20.  
    21.     public float turnTiltForcePercent = 1.5f;
    22.     public float turnForcePercent = 1.3f;
    23.  
    24.     public float maxEngineForce = 100;
    25.     private float _engineForce;
    26.  
    27.     bool JosyStickMode = true;
    28.  
    29.     public AnimationCurve EngineForceCurve = new AnimationCurve();
    30.  
    31.  
    32.     private void Start()
    33.     {
    34.         targetheight =  transform.position.y;
    35.     }
    36.  
    37.  
    38.    
    39.     public float EngineForce
    40.     {
    41.         get { return _engineForce; }
    42.         set
    43.         {
    44.             MainRotorController.RotarSpeed = value * 80;
    45.             SubRotorController.RotarSpeed = value * 40;
    46.             HelicopterSound.pitch = Mathf.Clamp(value / 40, 0, 1.2f);
    47.             //if (UIGameController.runtime.EngineForceView != null)
    48.              //   UIGameController.runtime.EngineForceView.text = string.Format("Engine value [ {0} ] ", (int)value);
    49.  
    50.             _engineForce = value;
    51.         }
    52.     }
    53.    
    54.  
    55.  
    56.     private Vector2 hMove = Vector2.zero;
    57.     private Vector2 hTilt = Vector2.zero;
    58.     private float hTurn = 0f;
    59.     public bool IsOnGround = true;
    60.  
    61.     bool forwardInput;
    62.      
    63.  
    64.     void FixedUpdate()
    65.     {
    66.         LiftProcess();
    67.         MoveProcess();
    68.         TiltProcess();
    69.     }
    70.  
    71.     private void MoveProcess()
    72.     {
    73.  
    74.         if (hMove.y != 0)
    75.         {
    76.             var turn = TurnForce * Mathf.Lerp(hMove.x, hMove.x * (turnTiltForcePercent - Mathf.Abs(hMove.y)), Mathf.Max(0f, hMove.y));
    77.         hTurn = Mathf.Lerp(hTurn, turn, Time.fixedDeltaTime * TurnForce);
    78.  
    79.         Heli_rb.AddRelativeTorque(0f, hTurn * Heli_rb.mass, 0f);  
    80.  
    81.         Vector3 moveVector = new Vector3(transform.forward.x, 0, transform.forward.z) * Mathf.Max(0f, hMove.y * ForwardForce * Heli_rb.mass);
    82.         Heli_rb.AddForce(moveVector);
    83.         }
    84.        
    85.         /*
    86.         else
    87.         {
    88.             Vector3 moveVector = Vector3.forward * Mathf.Max(0f, hMove.y * ForwardForce * HelicopterModel.mass);
    89.             HelicopterModel.AddRelativeForce(moveVector);
    90.         }
    91.         */
    92.     }
    93.  
    94.     private void LiftProcess()
    95.     {
    96.  
    97.         float curvePos = targetheight / MaxHeight;
    98.  
    99.         EngineForce = EngineForceCurve.Evaluate(Mathf.Abs(curvePos))*maxEngineForce;
    100.  
    101.  
    102.         var ratio = 1 - Mathf.Clamp(Heli_rb.transform.position.y / targetheight, 0, 1);
    103.         //Debug.Log("ratio  " + ratio);
    104.  
    105.         float  upForce = 0;
    106.  
    107.         Debug.Log("EngineForce  " + EngineForce);
    108.  
    109.         if (targetheight>0)  upForce = Mathf.Lerp(0f, EngineForce, ratio) * Heli_rb.mass;
    110.  
    111.         if (targetheight < 0) upForce = Mathf.Lerp(0f, -EngineForce, ratio) * Heli_rb.mass;
    112.  
    113.         //Debug.Log("upForce  " + upForce);
    114.  
    115.         Heli_rb.AddRelativeForce(Vector3.up * upForce);      
    116.     }
    117.  
    118.     private void TiltProcess()
    119.     {
    120.         currentForwardTiltForce = Mathf.Clamp(maxForwardTiltForce * forwardInputValue, 0, maxForwardTiltForce); // en función de cuanto input palante baja más o menos el morro.
    121.        
    122.         hTilt.x = Mathf.Lerp(hTilt.x, hMove.x * TurnTiltForce, Time.deltaTime);
    123.         hTilt.y = Mathf.Lerp(hTilt.y, hMove.y * maxForwardTiltForce, Time.deltaTime);
    124.         Heli_rb.transform.localRotation = Quaternion.Euler(hTilt.y, Heli_rb.transform.localEulerAngles.y, -hTilt.x);
    125.     }
    126.  
    127.  
    128.     private void Update()
    129.     {
    130.         if (JosyStickMode)
    131.         {
    132.             if (Input.GetKey(Input_mapping_manager.zoomIn_but))
    133.             {
    134.                 targetheight += Time.unscaledDeltaTime * 10f;
    135.             }
    136.  
    137.             if (Input.GetKey(Input_mapping_manager.zoomOut_but))
    138.             {
    139.                 targetheight -= Time.unscaledDeltaTime * 10f;
    140.                 if (EngineForce < 0) EngineForce = 0;
    141.             }
    142.  
    143.             if (!IsOnGround)
    144.             {            
    145.                 hMove.y = -Input.GetAxis(Input_mapping_manager.speed_axis);// - para invertirlo          
    146.                 hMove.x = Input.GetAxis(Input_mapping_manager.steer_axis);            
    147.  
    148.  
    149.                 if (Input.GetAxis(Input_mapping_manager.heli_turn_axis) != 0)
    150.                 {
    151.                     float inputDir = Input.GetAxis(Input_mapping_manager.heli_turn_axis); // en pad es o 1 o -1
    152.  
    153.                     var force = inputDir * (turnForcePercent - Mathf.Abs(Input.GetAxis(Input_mapping_manager.heli_turn_axis))) * Heli_rb.mass;
    154.                     Heli_rb.AddRelativeTorque(0f, force, 0);
    155.                 }
    156.             }
    157.         }
    158.        
    159.         else
    160.         {
    161.             if (Input.GetKey(Input_mapping_manager.speed_up_key))
    162.             {
    163.                 targetheight += Time.unscaledDeltaTime * 10f;
    164.             }
    165.  
    166.             if (Input.GetKey(Input_mapping_manager.speed_down_key) )
    167.             {
    168.                 targetheight -= Time.unscaledDeltaTime * 10f;
    169.                 if (EngineForce < 0) EngineForce = 0;
    170.             }
    171.  
    172.             if (Input.GetKey(Input_mapping_manager.speed_up_key))   hMove.y = 1;  
    173.             if (Input.GetKey(Input_mapping_manager.speed_down_key)) hMove.y = -1;
    174.  
    175.             if (Input.GetKey(Input_mapping_manager.turn_L_key)) hMove.x = -1;          
    176.             if (Input.GetKey(Input_mapping_manager.turn_R_key)) hMove.x = 1;          
    177.  
    178.  
    179.             if (!IsOnGround)
    180.             {
    181.                 hMove.y = -Input.GetAxis(Input_mapping_manager.speed_axis);// - para invertirlo          
    182.                 hMove.x = Input.GetAxis(Input_mapping_manager.steer_axis);
    183.  
    184.  
    185.                 if (Input.GetAxis(Input_mapping_manager.heli_turn_axis) != 0)
    186.                 {
    187.                     float inputDir = Input.GetAxis(Input_mapping_manager.heli_turn_axis); // en pad es o 1 o -1
    188.  
    189.                     var force = inputDir * (turnForcePercent - Mathf.Abs(Input.GetAxis(Input_mapping_manager.heli_turn_axis))) * Heli_rb.mass;
    190.                     Heli_rb.AddRelativeTorque(0f, force, 0);
    191.                 }
    192.             }
    193.         }
    194.     }
    195.  
    196.     private void OnCollisionEnter()
    197.     {
    198.         IsOnGround = true;
    199.     }
    200.  
    201.     private void OnCollisionExit()
    202.     {
    203.         IsOnGround = false;
    204.     }
    205. }

    Input is on a separate script, you need to modify this with unity key inputs or your input manager

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Input_mapping_manager : MonoBehaviour
    6. {
    7.  
    8.     //INPUT
    9.     int invertY = 1;
    10.     int invertX = 1;
    11.  
    12.     //keycodes
    13.     public static KeyCode speed_up_key;
    14.     public static KeyCode speed_down_key;
    15.     public static KeyCode turn_L_key;
    16.     public static KeyCode turn_R_key;
    17.  
    18.     public static KeyCode lock_brake_key;
    19.  
    20.     //axes
    21.     public static string speed_axis;
    22.     public static string steer_axis;
    23.  
    24.     //ZOOM INPUT
    25.     public static KeyCode zoomIn_but;
    26.     public static KeyCode zoomOut_but;
    27.     public static string Zoom_axis;
    28.  
    29.  
    30.     // Heli Turn
    31.     public static string heli_turn_axis;
    32.  
    33.  
    34.     // Start is called before the first frame update
    35.     void Start()
    36.     {
    37.         Input_Setup();
    38.     }
    39.  
    40.     void Input_Setup()
    41.     {
    42.         speed_up_key = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.speed_up].keyboardKey;
    43.         speed_down_key = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.speed_down].keyboardKey;
    44.         turn_L_key = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.turn_left].keyboardKey;
    45.         turn_R_key = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.turn_right].keyboardKey;
    46.  
    47.         lock_brake_key = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.lock_brake].keyboardKey;
    48.  
    49.         //axes
    50.         speed_axis = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.speed_control_axis].axis.ToString();
    51.         steer_axis = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.steer_control_axis].axis.ToString();
    52.  
    53.  
    54.         //ZOOM
    55.         zoomIn_but = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.zoom_in].gamepadBut;
    56.         zoomOut_but = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.zoom_out].gamepadBut;
    57.         Zoom_axis = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.zoom_axis].axis.ToString();
    58.  
    59.         //HELI SPECIFIC
    60.         heli_turn_axis = key_mapping_manager._kmm.playerKeyMapping[Km.actsEnm.heli_turn_axis].axis.ToString();
    61.     }
    62.  
    63.  
    64. }
     

    Attached Files:

  3. rickyivan

    rickyivan

    Joined:
    Sep 4, 2019
    Posts:
    1
    dude i get this error
    Input_mapping_manager.cs(42,24): error CS0103: The name 'key_mapping_manager' does not exist in the current context
    Input_mapping_manager.cs(60,68): error CS0103: The name 'Km' does not exist in the current context
    any idea how to fix them?
     
  4. AlexDalt

    AlexDalt

    Joined:
    Oct 17, 2013
    Posts:
    1
    1. public class Input_mapping_manager : MonoBehaviour
    Change input to Key