Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Basic to Advanced Car or Vehicle Tutorial

Discussion in 'Scripting' started by bigmisterb, Apr 22, 2011.

  1. lukewilliams

    lukewilliams

    Joined:
    Feb 11, 2014
    Posts:
    1
    For those struggling with the first tutorial, namely wheels going through the floor, you need to set the wheels differently to how he does it in the tutorial. the problem is that the wheels are set to rotation 0,0,90, and as soon as you rotate the wheel collider it stops working. So you need to figure out how to set the wheels to the right size without having them rotated 90 degrees. Then the wheel collider will be facing the right way and the wheels will stop at the ground :)
     
  2. MattBee

    MattBee

    Joined:
    Aug 23, 2013
    Posts:
    27
    You haven't used an array so just assign the wheel colliders from the hierarchy to the script when it's attached to your car then "GetCollider" would just be the name of the variable you have chosen which is "FrontLeft" "FrontRight "RearLeft" "RearRight"
     
  3. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Stucked at part1.

    The car is also flying (and flipping in the air) for me as soon as I press up/down.

    What could cause that?
     
  4. BestUnity

    BestUnity

    Joined:
    Aug 31, 2013
    Posts:
    2
    Last edited: Apr 19, 2014
  5. ViFFeX360

    ViFFeX360

    Joined:
    Jun 22, 2014
    Posts:
    1
    The codes that were said to be up to date on the site, aren't there anymore :/

    Is there anyone that still has the codes -with- explanation? I don't want a finished package, I just want to learn how it works to have a good car physics model, and no one can or wants to help on that aspect :/
     
  6. Unity Ninja

    Unity Ninja

    Joined:
    Jun 14, 2014
    Posts:
    14
    I can see this is a very old post but can I get the updated codes now? All the links here seem to be dead.
     
    Ali_V_Quest likes this.
  7. deadshot

    deadshot

    Joined:
    Oct 11, 2014
    Posts:
    1
    how can i change the textures of the different surfaces i.e.doors,hoods,bumpers etc on the click of a gui button.
    i can do this by keypress event or i can change the color of single surface but not the whole car .
    CAN YOU PLEASE TELL ME HOW TO O IT OR WRITE THE SCRIPT.
     
  8. fxnut

    fxnut

    Joined:
    Dec 17, 2014
    Posts:
    1
    To get the wheels to work, get rid of the 90 degree rotation on the spheres and change their scale to <0.0333, 1, 0.2>.

    Here's an updated script that seems to work for me. Once it compiles okay, you just need to drag and drop the correct wheels into the appropriate CarController script's wheel slots.

    Cheers,
    Andy

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //CarController1.js
    5.  
    6.  
    7. public class CarController : MonoBehaviour {
    8.  
    9.     public float enginePower = 150.0f;
    10.     public float power = 0.0f;
    11.     public float brake = 0.0f;
    12.     public float steer = 0.0f;
    13.     public float maxSteer = 25.0f;
    14.     public WheelCollider FrontLeft;
    15.     public WheelCollider FrontRight;
    16.     public WheelCollider RearLeft;
    17.     public WheelCollider RearRight;
    18.  
    19.     void Start()
    20.     {
    21.         rigidbody.centerOfMass=new Vector3(0f,-0.5f,0.3f);
    22.     }
    23.  
    24.     void Update ()
    25.     {
    26.         power=Input.GetAxis("Vertical") * enginePower * Time.deltaTime * 250.0f;
    27.         steer=Input.GetAxis("Horizontal") * maxSteer;
    28.         brake=Input.GetKey("space") ? rigidbody.mass * 0.1f: 0.0f;
    29.      
    30.         FrontLeft.steerAngle=steer;
    31.         FrontRight.steerAngle=steer;
    32.      
    33.         if(brake > 0.0)
    34.         {
    35.             FrontLeft.brakeTorque=brake;
    36.             FrontRight.brakeTorque=brake;
    37.             RearLeft.brakeTorque=brake;
    38.             RearRight.brakeTorque=brake;
    39.             RearLeft.motorTorque=0.0f;
    40.             RearRight.motorTorque=0.0f;
    41.         }
    42.         else
    43.         {
    44.             FrontLeft.brakeTorque=0;
    45.             FrontRight.brakeTorque=0;
    46.             RearLeft.brakeTorque=0;
    47.             RearRight.brakeTorque=0;
    48.             RearLeft.motorTorque=power;
    49.             RearRight.motorTorque=power;
    50.         }
    51.     }
    52.  
    53. }
    54.  
     
  9. AshTheTash

    AshTheTash

    Joined:
    Jul 4, 2015
    Posts:
    1
    Hey, I followed the tutorial excitedly to the end, when I got to the "Add the code to make it all work
    " section, unity unexpectedly crashed. It hadn't even saved. Would you be able to give me the project file for just the starting tutorial so I can actually see it?

    Cheers,
    Ash
     
  10. andrespsm

    andrespsm

    Joined:
    Jul 17, 2015
    Posts:
    1
    Hallo Guys,I have a problem from creating an script..I neaded a script for vehicle and a weels but an a litle time for these..I neaded a simple script for these. thanks,
     
  11. Figster

    Figster

    Joined:
    Mar 29, 2013
    Posts:
    10
    Will this work on two wheeled vehicles?
     
  12. tbm007

    tbm007

    Joined:
    Jan 9, 2017
    Posts:
    1
    Thanks for the great tutorial - and lots of attention to detail! I've followed the initial steps, built a basic box with four wheels, but no matter how I tweak the parameters (e.g. engine power etc) the car moves like molasses.. very slow. What might I be doing wrong?

    Also, after implementing the fixes for the wheel colliders sinking into the ground, the car is no longer stable. As soon as it hits the ground it leans either to the left (always to the left, never the right) or forward. The wheels are set up with identical values except for positioning so I don't think it's anything to do with how their colliders are configured. Again, any ideas would be helpful.

    I'm a unity noob, if that wasn't obvious - so it's highly likely I'm doing something daft even though I've tried to follow the tutorial pretty much precisely.
     
  13. Metalhawk93

    Metalhawk93

    Joined:
    Jul 13, 2016
    Posts:
    45
    say, I'm working on making a racing game right, but the graphics I want to use for my character are basically 2D sprites. Now I was wondering how I should set up my player. would it possibly work the same way if set up like a 3d model, or would I have to make some modifications?
    how I have my character set right now is that it's a parent attached with a box collider and a rigidbody with kinematic turned on, with a camera facing the character in a 3rd person perspective, the graphics, plus an empty name mark as children. I've yet to create a proper script for it as of yet, so I was wonder what ideas you guys might have.
     
  14. Tango91

    Tango91

    Joined:
    Oct 22, 2017
    Posts:
    1
    I've finished the first part of this excellent tutorial, but the required assets for the rest of the tutorials aren't available at the specified links. Any mirrors?

    Thanks :)
     
  15. baileyjohnston213

    baileyjohnston213

    Joined:
    Apr 30, 2018
    Posts:
    1
    it doesn't seem to work ive tried everything
     
  16. lulutube14

    lulutube14

    Joined:
    Sep 16, 2020
    Posts:
    12
    I tried all of the links, but they are scam links that makes it look like you have like a million virusus.
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,409
    Please don't post useless followups to 10-year-old threads. It's actually against forum rules and regulations.

    If you have a question yourself, make a new post... it's FREE!

    When you post, keep this in mind: How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/