Search Unity

Car wheels spinning and/or rotating?

Discussion in 'Scripting' started by carking1996, Feb 12, 2011.

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hello,

    How do I make car wheels spin and slow down when the car is stopped? And, How do I make the front wheels turn when pressing the left/right arrows? Help is very much appreciated. :)

    Thanks
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    How are you controlling the car? Are you using the built-in physics engine, or are you moving it manually via the transform component?
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    i am using this script:
    Code (csharp):
    1. var CurrentSpeed : float = 0;
    2. var RotationSpeed : float = 5.0;
    3.  
    4. function Update () {
    5.  
    6. transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);
    7.  
    8. if(CurrentSpeed < 0) {
    9. CurrentSpeed += 0.25 * Time.deltaTime;
    10. } else {
    11. CurrentSpeed -= 0.25 * Time.deltaTime;
    12. }
    13.  
    14. if(Input.GetKey(KeyCode.UpArrow)){
    15. CurrentSpeed += Time.deltaTime;
    16. }
    17.  
    18. if(Input.GetKey(KeyCode.DownArrow)){
    19. CurrentSpeed -= Time.deltaTime;
    20. }
    21.  
    22. if(Input.GetKey(KeyCode.LeftArrow)){
    23. transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    24. }
    25.  
    26. if(Input.GetKey(KeyCode.RightArrow)){
    27. transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    28. }
    29.  
    30. }
    Thanks for any help. :)
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You can figure out approximately how much the wheels need to rotate (setting aside differentials and stuff like that) based on the current speed and the wheel radius.

    This is off the top of my head so no guarantee of correctness, but it should look something like this (pseudocode - ish):

    Code (csharp):
    1. float distanceTraveled = speed * Time.deltaTime;
    2. float rotationInRadians = distanceTraveled / wheelRadius;
    3. float rotationInDegrees = rotationInRadians * Mathf.Rad2Deg;
    4. wheel.transform.Rotate(rotationInDegrees, 0, 0);
    5. // Or whatever axis is appropriate.
    For making the front wheels turn, you'll want to make them child objects and then modify their local rotations appropriately.
     
    vegn, ALIENPANDA, bariscigal and 2 others like this.
  5. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Use my script: If you what to:) have fun;)


    Code (csharp):
    1. var FRwheel : WheelCollider; //wheel front left collider
    2. var FLwheel : WheelCollider; //wheel front right collider
    3. var MaxmotorTorque = 200.0; //Max car engine speed
    4. var volate = 25;
    5. var MaxBrakeTorque = 500.0;
    6. private var Imbraking : boolean = false;
    7. var WheelsFL : Transform; // front left wheel mesh only ( no collider)
    8. var WheelsFR : Transform; // front right wheel mesh only ( no collider)
    9. var WheelsBL : Transform; // back left wheel mesh only ( no collider)
    10. var WheelsBR : Transform; // back right wheel mesh only ( no collider)
    11. //fix wheels problem dependins the axis.
    12. var fixX : float;
    13. var fixY : float;
    14. var fixZ : float;
    15. // Gui Text Speed of the car volues.
    16. var GuiSpeed : GUIText;
    17. //Max speed and Current Speed.
    18. var maxSpeed : float = 200.0;
    19. var maxBackSpeed : float = 40.0;
    20. var gearSpeed : int [];
    21. private var CurrentSpeed = 0.0;
    22. private var currentGear : int = 0;
    23. function Start()
    24. {
    25.    rigidbody.centerOfMass.y = 0;
    26. }
    27. function FixedUpdate ()
    28. {  
    29.    SteelWheels ();
    30.    GearSound();
    31.    GearDrive();
    32.  
    33.    FRwheel.steerAngle = volate  * Input.GetAxis("Horizontal");
    34.    FLwheel.steerAngle = volate  * Input.GetAxis("Horizontal");
    35.  
    36.    WheelsFL.Rotate(FLwheel.rpm / 60 * 360 * Time.deltaTime,0,0);
    37.    WheelsFR.Rotate(FLwheel.rpm / 60 * 360 * Time.deltaTime,0,0);
    38.    WheelsBL.Rotate(FLwheel.rpm / 60 * 360 * Time.deltaTime,0,0);
    39.    WheelsBR.Rotate(FLwheel.rpm / 60 * 360 * Time.deltaTime,0,0);
    40.  
    41.  
    42.    CurrentSpeed = (Mathf.PI * 2 * FLwheel.radius) * FLwheel.rpm * 60 / 1000;
    43.    CurrentSpeed = Mathf.Round(CurrentSpeed);
    44.  
    45.    if ((CurrentSpeed > 0)  (Input.GetAxis("Vertical") <0))
    46.    {
    47.         Imbraking = true;
    48.    }
    49.    else
    50.    {
    51.   Imbraking = false;
    52.   FLwheel.brakeTorque = 0;
    53.   FRwheel.brakeTorque = 0;
    54.    }  
    55.  
    56.    if ((CurrentSpeed < maxSpeed)  (CurrentSpeed > (maxBackSpeed * -1)))
    57.    {
    58.         FRwheel.motorTorque = MaxmotorTorque * Input.GetAxis("Vertical");
    59.         FLwheel.motorTorque = MaxmotorTorque * Input.GetAxis("Vertical");  
    60.    }
    61.    else
    62.    {
    63.         FLwheel.motorTorque = 0;
    64.   FRwheel.motorTorque = 0;
    65.    }
    66.    if (Imbraking == true)
    67.    {
    68.         FLwheel.brakeTorque = MaxBrakeTorque;
    69.         FRwheel.brakeTorque = MaxBrakeTorque;
    70.    }  
    71.    GuiSpeed.text = CurrentSpeed.ToString();
    72.  
    73. }
    74. function SteelWheels ()
    75. {
    76.   WheelsFL.localEulerAngles.y = FLwheel.steerAngle - WheelsFL.localEulerAngles.z;
    77.   WheelsFR.localEulerAngles.y = FRwheel.steerAngle - WheelsFR.localEulerAngles.z;
    78.  
    79.   //Wheels fix.Fix the front wheels spin sides.
    80.   fixX = WheelsFL.localEulerAngles.x;
    81.   fixY = WheelsFL.localEulerAngles.y;
    82.   fixZ = WheelsFL.localEulerAngles.z;  
    83.  
    84. }
    85. function GearSound()
    86. {
    87.     var tempoMinSpeed : float = 0.00;
    88.  var tempoMaxSpeed : float = 0.00;
    89.  var currentPitch : float = 0.00;
    90.  
    91.  switch (currentGear)
    92.  {
    93.         case 0:
    94.             tempoMinSpeed = 0.00;
    95.             tempoMaxSpeed = gearSpeed[currentGear];
    96.             break;
    97.  
    98.         default:
    99.             tempoMinSpeed = gearSpeed[currentGear -1];  
    100.          tempoMaxSpeed = gearSpeed[currentGear];
    101.  }
    102.  currentPitch = ((Mathf.Abs(CurrentSpeed) - tempoMinSpeed)/(tempoMaxSpeed - tempoMinSpeed)) +0.8;
    103.     audio.pitch = currentPitch;
    104. }
    105. function GearDrive()
    106. {
    107.     var gearNumber : int;
    108.  gearNumber = gearSpeed.length;
    109.  
    110.  for (var i=0; i< gearNumber; i++)
    111.  {
    112.      if(gearSpeed[i] > CurrentSpeed)
    113.   {  
    114.      currentGear = i;
    115.      break;  
    116.   }
    117.  }
    118. }
     
  6. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    It's alright. I am using the car script from the tutorial, since mine already sucks. :p
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Can you give a link to the tutorial (if you have any more questions we at least know where you are getting your information at)

    And... Isn't that what tutorials are for?
     
  8. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    The tutorial is the one in the unity resources page. ;)
     
  9. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    the script i post here is from german tutorials ;) I do some modification upgrade.
     
  10. davidmaggot

    davidmaggot

    Joined:
    Feb 8, 2011
    Posts:
    13
    Hi guys,

    I'm trying to make a wheel (yes only one wheel without the car and everything else) and make it move using the Iphone accelerometer... it's getting quite complicate to achive.

    I'm using an empty game object for the collider and a cylinder for the wheel... both are child of a parent gameObject with Rigidbody.

    Is the cylinder the right way to make the wheel ?
     
  11. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Your cylinder have the wheelCollider?
    If No, make sure you create an empty object and add the WheelCollider, and rescale the WhellCollider size whit your cylinder.
    Is not recomender add directely wheelCollider to an cylinder wheels or mesh wheels.
    Check the wheelCollider reference.
     
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This all depends on how you are accomplishing keeping the vehicle upright. With 1 wheel the first assumption is that it will fall over. Honestly, in your case I would use a wheel collider and use code to keep the vehicle upright instead of physics.

    And for what you are asking, write up some code and start a new thread. I believe that it warrants its own thread. ;)
     
  13. Nur Adeebah

    Nur Adeebah

    Joined:
    Dec 12, 2014
    Posts:
    1
    hi..i follow the script that you guys give but it doest work for me...
     
  14. khalil3353

    khalil3353

    Joined:
    May 19, 2015
    Posts:
    1
    Assets/Scripts/Car Control.js(56,22): BCE0077: It is not possible to invoke an expression of type 'boolean'.
     
  15. unity_xog2ktNwvnUW4A

    unity_xog2ktNwvnUW4A

    Joined:
    Aug 12, 2018
    Posts:
    1




    For C# ?
     
  16. ALIENPANDA

    ALIENPANDA

    Joined:
    Jan 14, 2020
    Posts:
    32
    Thank You, I was searching for one without a wheel colliders.