Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Car Dyno/Burnout simulation wheel rotation

Discussion in 'Scripting' started by 8r3nd4n, Apr 23, 2013.

  1. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Hi all,

    I am trying to make a car dyno where a vehicle stays stationary while the wheels accelerate and rotate faster as the car reaches high speeds.
    To see a dyno in action: http://www.youtube.com/watch?v=N1Zwa07f0rg

    I have used the car tutorial and I would have liked to use it but I cant seem to get the car to stay stationary. If I turn the rigidbody to kinematic, the wheels dont behave properly I think due to the fact that the wheel colliders need something to interact with to cause friction. There is also a lot of functionality in there that is not needed so I thought it would be easy to just have a script that rotates the rear wheels without the need for wheel colliders.

    In psuedocode I think it would go like this:

    When the up button is held the wheel starts rotating around the z axis.
    The longer the button is held, the faster the wheel rotates.
    When the button is released, the acceleration stops and the wheel keeps spinning until it slows and stops naturally.

    I have got it so that when the button isnt pressed, the wheel will gradually return to stationary but dont think it looks that natural. I read that you could use add torque to cause it to behave right but it doesnt seem to work. Heres my code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpinWheels : MonoBehaviour {
    5.    
    6.      public float speedUp = 0.0F;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void FixedUpdate () {
    14.         if(Input.GetKey("up"))
    15.         {
    16.             speedUp += 0.1F;
    17.             transform.Rotate(Vector3.right * speedUp); 
    18.            
    19.         }
    20.         else
    21.         {
    22.             //rigidbody.AddTorque( Vector3.forward);
    23.             transform.Rotate(Vector3.right * speedUp); 
    24.             speedUp -= 0.1F;
    25.         }
    26.        
    27.         if (speedUp <=0.0F)
    28.         {
    29.             speedUp = 0.0F;
    30.         }
    31.    
    32.     }
    33.    
    34. }
    Also, any idea as to how to add gears into the mix, so that when a second button is pressed, we can go up a gear and faster ( I realise that I will probably just need to put a max value on speedUp for each gear setting but wondering if there would be a better way to do it)?

    Thanks
     
  2. Scottty

    Scottty

    Joined:
    Apr 2, 2015
    Posts:
    2
    Hi,
    Did you have any luck with this script?
    Did you get it to work?

    Thanks
    Scottty