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. Dismiss Notice

Need Help PlayerControler script C#

Discussion in 'Scripting' started by Toxic_Sky, Feb 3, 2015.

  1. Toxic_Sky

    Toxic_Sky

    Joined:
    Aug 2, 2014
    Posts:
    2
    I am following this tutorial series on youtube and can't figure out what im doing wrong. I get no console errors when run it, but it does not seem to work. I suspect something is wrong with the CheckInput() function. I have set hot keys within the editor "edit/project settings/input", added Axes for Thrust. Horizontal and Vertical were already there.

    If you can provide me with any help whatsoever it would be greatly appreciated !! Thanks in advance!!

    Sorry code is not commented :-/

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour {
    5.  
    6.     public float maxTurnRate = 1200f;
    7.     public Vector3 maxImpulse = new Vector3 (10f, 10f, 700f);
    8.     public Vector3 velocity = Vector3.zero;
    9.     public float impulseSensitivity = 500f;
    10.     public float turnSensitivity = 1200f;
    11.    
    12.     public ParticleSystem leftExhaustFX;
    13.     public ParticleSystem rightExhaustFX;
    14.    
    15.     private Vector3 impulse = Vector3.zero;
    16.     private float desiredImpulse = 0f;
    17.     private Vector3 impulseActual = Vector3.zero;
    18.     private float maxImpulseChange = 100f;
    19.     private Vector3 turnRate = Vector3.zero;
    20.     private float desiredImpulseInput = 0f;
    21.     private float desiredTurnXInput = 0f;
    22.     private float desiredTurnYInput = 0f;
    23.     private float desiredInputX = 0f;
    24.     private float desiredInputY = 0f;
    25.     private float desiredTurnX = 0f;
    26.     private float desiredTurnY = 0f;
    27.  
    28.     public Transform thisTransform;
    29.     public float enginePowerValue = 0f;
    30.     public GUIText enginePercentageText;
    31.  
    32.     public Vector3 Velocity
    33.         {
    34.         get
    35.         {
    36.             return velocity;
    37.         }
    38.     }
    39.     public Vector3 Impulse
    40.                 {
    41.                 get
    42.                 {
    43.                     return impulse;
    44.                 }
    45.                 set
    46.                 {
    47.                         impulse.x = Mathf.Clamp (value.x, 0, maxImpulse.x);
    48.                         impulse.y = Mathf.Clamp (value.y, 0, maxImpulse.y);
    49.                         impulse.z = Mathf.Clamp (value.z, 0, maxImpulse.z);
    50.                 }
    51.         }
    52.     // Use this for initialization
    53.     void Start ()
    54.     {
    55.            
    56.         thisTransform = transform;
    57.     }
    58.  
    59.     void onEnginePowerChange() {
    60.  
    61.         if (enginePercentageText != null)
    62.         {
    63.             enginePowerValue = (desiredImpulse / maxImpulse.z * 100) / 100f;
    64.  
    65.             enginePercentageText.text ="Engines" + " " + (enginePowerValue * 100).ToString("f0") + "%";
    66.         }
    67.  
    68.     }
    69.  
    70.     void AdjustEngineFX() {
    71.         if (leftExhaustFX != null && rightExhaustFX != null)
    72.         {
    73.             leftExhaustFX.startSize = 1 + enginePowerValue * 2f;
    74.             rightExhaustFX.startSize = 1 + enginePowerValue * 2f;
    75.  
    76.             leftExhaustFX.startSpeed = 1 + enginePowerValue * 3f;
    77.             rightExhaustFX.startSpeed = 1 + enginePowerValue * 3f;
    78.         }
    79.  
    80.     }
    81.     // Update is called once per frame
    82.     void Update () {
    83.  
    84.         onEnginePowerChange ();
    85.         AdjustEngineFX ();
    86.  
    87.         thisTransform.Rotate (turnRate * Time.deltaTime, Space.Self);
    88.            
    89.         if (Vector3.Distance (impulse, impulseActual) < maxImpulseChange * Time.deltaTime)
    90.         {
    91.             impulseActual = impulse;      
    92.         }
    93.         else
    94.         {
    95.             impulseActual += (impulse - impulseActual).normalized * maxImpulseChange * Time.deltaTime;              
    96.         }
    97.             velocity = thisTransform.rotation * impulseActual / 10f;
    98.             thisTransform.Translate (velocity * Time.deltaTime, Space.World);
    99.         }
    100.  
    101.         void CheckInput()
    102.         {
    103.             if (this == null)
    104.             {
    105.             return;
    106.             }
    107.  
    108.         desiredImpulseInput = 0f;
    109.         desiredTurnXInput = 0f;
    110.         desiredTurnYInput = 0f;
    111.         desiredTurnX = 0f;
    112.         desiredTurnY = 0f;
    113.  
    114.         desiredImpulseInput += Input.GetAxis ("Thrust") * impulseSensitivity * Time.deltaTime;
    115.         desiredImpulse = (Mathf.Clamp (desiredImpulseInput, GetImpulse2 (), GetMaxImpulse2 ()));
    116.         desiredImpulse += desiredImpulseInput;
    117.  
    118.         desiredTurnXInput += Input.GetAxis ("Vertical") * turnSensitivity * Time.deltaTime;
    119.         desiredTurnX = (Mathf.Clamp (desiredTurnXInput, -GetMaxTurnRate(), GetMaxTurnRate ()));
    120.         desiredTurnX += desiredTurnXInput;
    121.  
    122.         desiredTurnYInput += Input.GetAxis ("Horizontal") * turnSensitivity * Time.deltaTime;
    123.         desiredTurnY = (Mathf.Clamp (desiredTurnYInput, -GetMaxTurnRate(), GetMaxTurnRate ()));
    124.         desiredTurnY += desiredTurnYInput;
    125.  
    126.         SetImpulse2 (desiredImpulse);
    127.         SetTurnRate (desiredTurnX, desiredTurnY, 0);
    128.  
    129.     }
    130.     public void SetImpulse2 (float z)
    131.     {
    132.         Impulse = new Vector3 (0,0,z);
    133.     }
    134.  
    135.     public float GetImpulse2()
    136.     {
    137.         return impulse.z;
    138.     }
    139.  
    140.     public float GetMaxImpulse2()
    141.     {
    142.         return maxImpulse.z;
    143.     }
    144.  
    145.     public void SetTurnRate (float x, float y, float z)
    146.     {
    147.         turnRate.x = Mathf.Clamp (x, - maxTurnRate, maxTurnRate);
    148.         turnRate.y = Mathf.Clamp (y, - maxTurnRate, maxTurnRate);
    149.         turnRate.z = Mathf.Clamp (z, - maxTurnRate, maxTurnRate);
    150.     }
    151.  
    152.     public void SetTurnRate (Vector3 v)
    153.     {
    154.         SetTurnRate (v.x, v.y, v.z);
    155.     }
    156.  
    157.     public Vector3 GetTurnRate()
    158.     {
    159.         return turnRate;
    160.     }
    161.  
    162.     public float GetMaxTurnRate()
    163.     {
    164.         return maxTurnRate;
    165.     }
    166.  
    167.  
    168.  
    169. }
    170.  
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    My suggestion is to first try putting Debug.Log statements in the places where you expect something to happen..like
    if you think CheckInput could have problems, then Debug.Log there and print out the values
    and see how it goes.
     
    Toxic_Sky likes this.
  3. Toxic_Sky

    Toxic_Sky

    Joined:
    Aug 2, 2014
    Posts:
    2
    Thanks, i will try that.