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

vehicle speed goes up when no key is pressed

Discussion in 'Scripting' started by ghost123_1234, Nov 21, 2015.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello

    i have this code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class carcontroller : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public WheelCollider WheelFL;
    10.     public WheelCollider WheelFR;
    11.     public WheelCollider WheelRR;
    12.     public WheelCollider WheelRL;
    13.     public Transform WheelFLTrans;
    14.     public Transform WheelFRTrans;
    15.     public Transform WheelRLTrans;
    16.     public Transform WheelRRTrans;
    17.  
    18.  
    19.  
    20.     private float decellarationSpeed = 2500;
    21.     public float currentSpeed = 0;
    22.     private float topspeed = 180;
    23.     private float maxReverseSpeed = 30;
    24.     private float LowestSteeratSpeed = 60;
    25.     private float lowspeedsteerangel = 20;
    26.     private float highspeedsteerangel = 5;
    27.     public int[] gearRatio = new int[0];
    28.  
    29.     private float topkmhspeed;
    30.  
    31.  
    32.     //snelheidsmeter
    33.     public Texture2D speedOmeterDial;
    34.     public Texture2D speedOmeterPointer;
    35.  
    36.  
    37.  
    38.  
    39.  
    40.     float maxTorque = 5000;
    41.     public Vector3 angleright;
    42.  
    43.     //lichten
    44.     private bool braked = false;
    45.     public Material brakeLightMaterial;
    46.     public Material reverseLightMaterial;
    47.     public GameObject backLightObject;
    48.     public Material idleLightMaterial;
    49.  
    50.     private Rigidbody rb;
    51.  
    52.     private RaycastHit hit;
    53.  
    54.  
    55.     void Start()
    56.     {
    57.  
    58.  
    59.         rb = GetComponent<Rigidbody>();
    60.         rb.centerOfMass = new Vector3(0, -0.5f, 0.3f);
    61.     }
    62.  
    63.     void FixedUpdate()
    64.     {
    65.         Controle();
    66.     }
    67.     void Update()
    68.     {
    69.         topkmhspeed = 180;
    70.         WheelPosition();
    71.         //  EngineSound();
    72.         WheelFLTrans.Rotate(WheelFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
    73.         WheelFRTrans.Rotate(WheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
    74.         WheelRRTrans.Rotate(WheelRR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
    75.         WheelRLTrans.Rotate(WheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
    76.  
    77.         Vector3 WheelFLTransAngle = WheelFLTrans.localEulerAngles;
    78.         Vector3 WheelFRTransAngle = WheelFRTrans.localEulerAngles;
    79.  
    80.         WheelFLTransAngle.y = WheelFL.steerAngle - WheelFLTrans.localEulerAngles.z;
    81.         WheelFRTransAngle.y = WheelFR.steerAngle - WheelFRTrans.localEulerAngles.z;
    82.  
    83.         WheelFRTrans.localEulerAngles = WheelFRTransAngle;
    84.         WheelFLTrans.localEulerAngles = WheelFLTransAngle;
    85.         // BackLight();
    86.     }
    87.  
    88.     void Controle()
    89.     {
    90.  
    91.         currentSpeed = rb.velocity.magnitude * 3.6f;
    92.         currentSpeed = Mathf.Round(currentSpeed);
    93.         if (currentSpeed <= topspeed && currentSpeed > -maxReverseSpeed)
    94.         {
    95.             WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    96.             WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    97.         }
    98.         else
    99.         {
    100.             WheelRR.motorTorque = 0;
    101.             WheelRL.motorTorque = 0;
    102.         }
    103.  
    104.         if (Input.GetKey(KeyCode.Space))
    105.         {
    106.             WheelRR.brakeTorque = decellarationSpeed;
    107.             WheelRL.brakeTorque = decellarationSpeed;
    108.             braked = true;
    109.         }
    110.         else
    111.         {
    112.             braked = false;
    113.             WheelRR.brakeTorque = 0;
    114.             WheelRL.brakeTorque = 0;
    115.         }
    116.  
    117.         var speedFactor = GetComponent<Rigidbody>().velocity.magnitude / LowestSteeratSpeed;
    118.         var currentsteerangle = Mathf.Lerp(lowspeedsteerangel, highspeedsteerangel, speedFactor);
    119.         currentsteerangle *= Input.GetAxis("Horizontal");
    120.  
    121.  
    122.         WheelFL.steerAngle = currentsteerangle;
    123.         WheelFR.steerAngle = currentsteerangle;
    124.  
    125.     }
    126.  
    127.  
    128.     void WheelPosition()
    129.     {
    130.      
    131.     }
    132.  
    133.     void EngineSound()
    134.     {
    135.         int gears = gearRatio.Length;
    136.         int i;
    137.         for (i = 0; i < gears; i++)
    138.         {
    139.             if (gearRatio[i] > currentSpeed)
    140.             {
    141.                 break;
    142.             }
    143.         }
    144.         float gearMinValue = 0.00f;
    145.         float gearMaxValue = 0.00f;
    146.         if (i == 0)
    147.         {
    148.             gearMinValue = 0;
    149.         }
    150.         else
    151.         {
    152.             gearMinValue = gearRatio[i - 1];
    153.         }
    154.         gearMaxValue = gearRatio[i];
    155.         float enginePitch = ((currentSpeed - gearMinValue) / (gearMaxValue - gearMinValue)) + 1;
    156.         GetComponent<AudioSource>().pitch = enginePitch;
    157.  
    158.  
    159.     }
    160.     void OnGUI()
    161.  
    162.     {
    163.  
    164.  
    165.         GUI.DrawTexture(new Rect(Screen.width - 300, Screen.height - 150, 300, 150), speedOmeterDial);
    166.         float speedFactor = currentSpeed / topspeed;
    167.         float rotationAngle;
    168.         if (currentSpeed >= 0)
    169.         {
    170.             rotationAngle = Mathf.Lerp(0, 180, speedFactor) - 10;
    171.         }
    172.         else
    173.         {
    174.             rotationAngle = Mathf.Lerp(0, 180, -speedFactor);
    175.         }
    176.         GUIUtility.RotateAroundPivot(rotationAngle, new Vector2(Screen.width - 150, Screen.height));
    177.         GUI.DrawTexture(new Rect(Screen.width - 300, Screen.height - 150, 300, 300), speedOmeterPointer);
    178.  
    179.     }
    180.  
    181.     void BackLight()
    182.     {
    183.         if (currentSpeed > 0 && Input.GetAxis("Vertical") < 0 && !braked)
    184.         {
    185.             backLightObject.GetComponent<Renderer>().material = brakeLightMaterial;
    186.         }
    187.         else if (currentSpeed < 0 && Input.GetAxis("Vertical") > 0 && !braked)
    188.         {
    189.             backLightObject.GetComponent<Renderer>().material = brakeLightMaterial;
    190.         }
    191.         else if (currentSpeed < 0 && Input.GetAxis("Vertical") < 0 && !braked)
    192.         {
    193.             backLightObject.GetComponent<Renderer>().material = reverseLightMaterial;
    194.         }
    195.         else if (!braked)
    196.         {
    197.             backLightObject.GetComponent<Renderer>().material = idleLightMaterial;
    198.         }
    199.     }
    200. }
    201.  
    202.  
    203.  
    But when i let go of like the W key the car just speeds up when i let go of like 30 KMH the speed goes to 40 KMH

    When i debug the motortorque when i let go the key the motortorque goes back to 0
    Someone knows how to fix this?
     
  2. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    i got a feeling that u have done something like saying IF W is not pressed then speed goes up? u need to fiqure that out since its most likely somewhere with controlls u messed up
     
  3. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    the code in my opening post is my howl code for the car i dont use any other code and i dont have any other script in my project file yet
     
  4. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
  5. btft

    btft

    Joined:
    Aug 11, 2015
    Posts:
    14
    Wow, that's hell of a class.
    Takes responsibilities of both View and Model along with responsibilities of Controller. :)
    Mind sharing a scene so we don't need to setup everything directly as you have (which is probably impossible)?
     
  6. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
  7. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88