Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Basic to Advanced Car or Vehicle Tutorial

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

  1. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    Still Not working as you can see all my tires are in it. still SAY'S its moving when its not.

     
  2. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    Does each wheel have the wheel script attached to them? Are your axes oriented correctly on the wheels? The transforms need to have the Z arrow out the front of them and the X arrow out the right side of them.

    On your car body, your vector indicators show that the Z axis is going out the back of the car and the X axis is going out the left side. If the transform isn't oriented correctly in the world, the scripts will do the math, and the output will be applied wrongly to your rigidbody and colliders.

    BigMisterB, thanks for the code on the live axle/Independent setups. I was going to be getting to this pretty soon, but got sidetracked on developing some of my models and my environment. And I never intended to do a tutorial on it. Because I'm evil or something like that, I do believe.

    I did actually write up a tutorial on a nice motorcycle setup, but unfortunately the code and the models were purchased from me at a time of weakness(gotta pay the rent somehow), and I'm not allowed to use them or reveal them anymore. But now I'm not going to tell that guy how to make a swingarm for the bike. I did say I'm evil, didn't I?

    This is all very good stuff. I have tried everyone's tutorials, hacked 'em up pretty good myself, sprinkled in plenty of spices from the Unity Team's Alternate Physics thingy, but honestly, I think you've got the best approach to this problem for most people. I always end up going back to Andrew Gotow's methods and work from there. The bike was easy. Making a convincing car is a lot harder.
     
    Last edited: May 24, 2011
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    In that section of the tutorial the only thing that mattered was the engine power and max steering. Power, Brake and Steer are all controlled by script. (These are hidden in the later scripts).

    What I cannot see is the radius of the wheels. And oddly, they look small. It looks as if they are not large enough to extend past the main collier.

    Did you create a collider, if so, create a picture of it highlighted. Check then to see if the radius of each wheel extends past that collider.

    I see in your images that the order of your wheels is: RL, FR, FL, RR. In the tutorial and code, the assumption is that they are FFRR. (front front rear rear) so it would steer the wrong wheels in your setup.

    Lastly, if it doesn't seem to be moving, change the engine power to something stupid like 100000. If that isnt enough power to move the wheels, nothing is... lol
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, my fellow Unitites.... I think this is pretty much the last of the tutorial. Of course the DEMO has been updated and the code is here: http://lod3dx.net/Unity/

    Basic AI

    The concept of the basic AI I am providing with this is really a rudimentary waypoint AI. It knows where it has to be, it points the wheels in that direction and mashes the gas. Quite simple. This is all part of driving though. The point for me giving a simple AI is so that people who look at it and try to read it can. If it were so complex that it couldn't be followed... well there wouldn't be much point in putting it into a tutorial would there?

    I will attempt to break down the AI so that people can understand it though.. :D

    OK, to start, I am not going to go over every variable, the point of naming things correctly is so that you know that lastPosition, really is the last position that we took.. ;)

    Code (csharp):
    1.  
    2. var timidness01 = 0.5; // amount of proportional brake that the AI applies on tighter turns
    3. var concentration01 = 0.8; // percentage of ability to remain on the same waypath
    4.  
    These two variables handle how the vehicle reacts to certain events. The demo project only represents one set of waypoints. Concentration would be used to handle more than one set of waypoints though. So as much as I think it is great, it is fairly useless in the grand scheme of things. Timidness, though not fully implemented in this demo is the ability for the driver to slow down when he knows sharp turns are coming.

    Code (csharp):
    1.  
    2. function doDriving(){ // drive towards the target
    3.     var position=transform.InverseTransformPoint(waypoint.transform.position);
    4.     var angle=Vector3.Angle(Vector3.forward, position);
    5.     angle=Vector3.Dot(Vector3.right, position)>0 ? angle : -angle;
    6.    
    7.     //define the steering, left or right
    8.     steer=-1.0;
    9.     if(angle>=0)steer=1.0;
    10.    
    11.     drive=1.0;
    12.     if(Mathf.Abs(angle)>120.0){
    13.         drive=-1.0;
    14.         steer=-steer;
    15.     }
    16.    
    17.     steer=Mathf.Clamp(Mathf.Abs(angle),0.0,VC.veloSteer)*steer/VC.veloSteer;
    18.    
    19.     // reduce drive if our angle is above the amount that we want to steer
    20.     if(drive>0.0){
    21.         position=transform.InverseTransformPoint(waypoint.next.transform.position);
    22.         var angle2=Vector3.Angle(Vector3.forward, position);
    23.         angle2=Vector3.Dot(Vector3.right, position)>0 ? angle2 : -angle2;
    24.         var mps=rigidbody.velocity.magnitude;
    25.        
    26.         if(Mathf.Abs(angle + angle2)/2>VC.veloSteer/3)
    27.             drive= 1-timidness01;
    28.     }
    29.    
    30.     VC.inputSteer=steer;
    31.     VC.inputPower=drive;
    32. }
    33.  
    This is the workhorse of the AI. This is the part that tells the vehicle to mash the gas, or turn the wheel. In this example it solely deals with the waypoints. Drives as fast as it can to them, turns to the next and so on. As you can see it takes the angle of the vehicle in relation to the waypoint and matches it against the speed. In the end it then assigns the steer and drive of the vehicle, as if it were pressing buttons.

    I am not putting the update here as it is nothing but a bunch of checks for position and relation to waypoints. There is a couple of timer checks. It does check to see if we are sitting in the same place for over 3 seconds. This means we are probably stuck. Also, it checks to see if we haven't made the check point in 30 seconds, if so, we just reset to the last one, and point towards the next.

    More Environment
    I also attached a bit more environment and sounds to the demo. Scrape and crash noises as well as smoke and spark effects.

    Hopefully this entire project is useful to someone. It does document a lot of features I have found in Unity and though it doesnt give the best solutions, they are for sure some of the easiest ones.

    If I do continue working on this I may implement a different physics model for the wheels. WheelColliders lack in some of the finer aspects.
     
    Last edited: Jul 1, 2011
  5. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    I played the demo, and it looks great!
     
  6. aninjamonk

    aninjamonk

    Joined:
    Aug 31, 2010
    Posts:
    153
  7. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Mark it...so cool..Thanks for share.!
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This was discussed before, I dont know why it's doing that, the link is not that. I also gave the exact URL for it too... ;)
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    lol.. does no one know how to copy and paste? The whole URL is posted where the link is. Its right, it works.. ;)

    http://lod3dx.net/Unity/
     
    Last edited: Jul 1, 2011
  11. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  12. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    still not working for me.....
     
  13. aninjamonk

    aninjamonk

    Joined:
    Aug 31, 2010
    Posts:
    153
    thanks... it works..
     
  14. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    still not working for me :( guess its just not my day.
     
  15. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    anyonee?????
     
  16. flim

    flim

    Joined:
    Mar 22, 2008
    Posts:
    326
    Wow just found this tutorial. Thank you so much!

    The link works, if not, press F5 to refresh.
     
  17. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    dude... i cant get it too move..
     
  18. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Please be more specific. "dude... i cant get it to move." doesn't really describe your problem.

    Do you not understand what all needs to happen? You need a model, a collision mesh, and 4 wheels. Start at the beginning of this entire thread and work from there. It is a basic car, no special stuff. What is provided in the zip is all the scripts it took to make the demo work... Which is alot. It does not, however give you any of the things that make it run.

    You require A game controller, an environment controller a vehicle as described before.

    It is NOT simple, it is advanced. ;)
     
  19. LastBulliet

    LastBulliet

    Joined:
    Apr 4, 2011
    Posts:
    91
    Where do i download that zip?
     
  20. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  21. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    I don't know what's wrong with my Unity, but when i add wheel colider it's 90 degrees rotated relative to wheel itself.
     
  22. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    In your 3d program, you need to have the car facing forward and the wheel center must be in the center of the wheel, and it must also be pointing forward.

    What 3d app are you using?
     
  23. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    None, just following intructions with Unity editor :)
    I forgot to mention that if i scale it on X scale (not Y like in first post), colider position and wheel appearance match.
     
  24. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Minor Update:

    I posted the entire project up on my site. Download it, put it in a folder, load level1 and it should play right.

    http://lod3dx.net/Unity/
     
  25. MANISH KUMAR

    MANISH KUMAR

    Joined:
    Nov 22, 2010
    Posts:
    57

    Please , Give me the full information mean statement of each line of code...mainly which is in green color please...
    This codes come from car tutorial

     
  26. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This code is not from my tutorial.. as a matter of fact is a mathematical representation of physics and doesn't conform to any of the guidelines of this tutorial.

    Though in concept is is fairly nice. It is not a true physics vehicle:

    Code (csharp):
    1.  
    2. function ApplySteering(canSteer : boolean, relativeVelocity : Vector3){
    3.     if(canSteer){
    4.         // evaluate the current steering capability
    5.         var turnRadius : float = 3.0 / Mathf.Sin((90 - (steer * 30)) * Mathf.Deg2Rad);
    6.         Debug.Log(""+Mathf.Sin((90 - (steer * 30)) * Mathf.Deg2Rad));
    7.        
    8.         // get the max steer based off of the velocity of the vehicle
    9.         var minMaxTurn : float = EvaluateSpeedToTurn(rigidbody.velocity.magnitude);
    10.         // clamp the steer speed to the maximum turn rate
    11.         var turnSpeed : float = Mathf.Clamp(relativeVelocity.z / turnRadius, -minMaxTurn / 10, minMaxTurn / 10);
    12.        
    13.         // preform a transform to rotate the vehicle around the right side in the distance of the turning radius
    14.         transform.RotateAround( transform.position + transform.right * turnRadius * steer,
    15.             transform.up,
    16.             turnSpeed * Mathf.Rad2Deg * Time.deltaTime * steer);
    17.  
    18.         var debugStartPoint = transform.position + transform.right * turnRadius * steer;
    19.         var debugEndPoint = debugStartPoint + Vector3.up * 5;
    20.  
    21.         Debug.DrawLine(debugStartPoint, debugEndPoint, Color.red);
    22.        
    23.         // setup a braking where you can brake the vehicle left or right based off the hand brake
    24.         // to do this we use the current steering to modifiy the rotation of the vehicle
    25.         // if no steering is currently used, use a random rotation.
    26.         if(initialDragMultiplierX > dragMultiplier.x){ // Handbrake
    27.             var rotationDirection : float = Mathf.Sign(steer); // rotationDirection is -1 or 1 by default, depending on steering
    28.             if(steer == 0){
    29.                 if(rigidbody.angularVelocity.y < 1) // If we are not steering and we are handbraking and not rotating fast, we apply a  random rotationDirection
    30.                     rotationDirection = Random.Range(-1.0, 1.0);
    31.                 else
    32.                     rotationDirection = rigidbody.angularVelocity.y; // If we are rotating fast we are applying that rotation to the car
    33.             }
    34.             // -- Finally we apply this rotation around a point between the cars front wheels.
    35.             transform.RotateAround(
    36.                 transform.TransformPoint(( frontWheels[0].localPosition + frontWheels[1].localPosition) * 0.5),
    37.                 transform.up,
    38.                 rigidbody.velocity.magnitude * Mathf.Clamp01(1 - rigidbody.velocity.magnitude / topSpeed) * rotationDirection * Time.deltaTime * 2);
    39.         }
    40.     }
    41. }
    42.  
     
  27. MANISH KUMAR

    MANISH KUMAR

    Joined:
    Nov 22, 2010
    Posts:
    57

    Ok,thanks BigMisterB , i learn both tutorial.and apply than i check it...
    But this code is used professionally....
    but I accept ur tutorial also...


    Thanks
     
  28. MANISH KUMAR

    MANISH KUMAR

    Joined:
    Nov 22, 2010
    Posts:
    57

    Hello Sir,
    Your tutorial is nice but here , the problem is that ur car movement is sliding highly , not manual working nice.....
    Code is nice due to , you individual component add and easily understanding....
    but difficult to play a car,not easily handling....


    Please solve this.
     
  29. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    All handling characteristics are through model and numbers. I can't "solve" this, because I am not using your models. The code is a reference on how to get started, how you finish, is up to you.
     
  30. dfb

    dfb

    Joined:
    Aug 16, 2011
    Posts:
    14
    hrm... i think i missed something b/c i did everything the first post said to do, but my car wheels sink through the terrain instaed of staying on top. this may be because i used a 'cube' instead of a 'box' but i canot find a 'box'
     
  31. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    cube==box

    Are your wheels linked to the box and does each wheel have a wheel collider? Are the wheel colliders assigned to the script?
     
  32. dog

    dog

    Joined:
    Aug 19, 2010
    Posts:
    24
    it's reporting the following:


    Assets/VehicleController/EnvironmentController.js(364,64): BCE0022: Cannot convert 'UnityEngine.Component[]' to 'UnityEngine.Component'.


    Assets/VehicleController/WheelController.js(165,64): BCE0022: Cannot convert 'UnityEngine.Component[]' to 'UnityEngine.Component'.
     
  33. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Both of those suffer from a similar error, dont know why they didnt give me that error before.

    It should read something like this:
    var objs : Component[] = inObject.GetComponentsInChildren(Transform);

    Notice: Component[]

    Code (csharp):
    1.  
    2. function isInViewFrustrum(testObject : GameObject){
    3.     var visible=false;
    4.     var objs : Component[]=testObject.GetComponentsInChildren(Renderer);
    5.     for(var obj : GameObject in objs){
    6.         if(obj.renderer.isVisible)visible=true;
    7.     }
    8.     if(testObject.renderer)
    9.         if(testObject.renderer.isVisible) visible=true;
    10.     return visible;
    11. }
    12.  
    Code (csharp):
    1.  
    2. function FindChild(name : String, inObject : GameObject){
    3.     var objs : Component[] = inObject.GetComponentsInChildren(Transform);
    4.     for(var obj : Component in objs){
    5.         if(obj.name==name) return obj.gameObject;
    6.     }
    7.     return null;
    8. }
    9.  
     
  34. CharlieP

    CharlieP

    Joined:
    Jun 5, 2012
    Posts:
    1
    Great work, just what I'm looking for to learn from :)

    I have downloaded the zip file from your site so I can play with the code and see how it all fits together, but I cannot run it with that latest unity version. I get the errors in above post and your fixes seem to work on the wheel script only, the other still errors.

    Any chance you can check the zip and update it if necessary?

    Pretty please :D
     
    Last edited: Jun 5, 2012
  35. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    458
    Hiya,

    For anyone else who is having trouble getting the CarTutorial.zip code to work in more recent versions of Unity, here are the steps I took to get it functioning...

    1) Open all included scripts and change all private variables to internal variables.

    2) In both EnvironmentController.js and WheelController.js, search for var objs : Component etc etc, and replace the two instances with var objs : Component[] etc etc...

    3) Open WheelController.js and search for the line for(var obj : Transform in transform){count++;}. Change obj to something else, for instance objA.

    If all goes well, you should be able to run the demo error free... These "fixes" more than likely break some of the original functionality, but so far it's worked for me error free.

    Have fun!
    - Ken
     
  36. corei5

    corei5

    Joined:
    Sep 28, 2012
    Posts:
    1
    im change function but error....
     
  37. BusterBlader

    BusterBlader

    Joined:
    Apr 23, 2012
    Posts:
    79
    Nice tutorial ;) I downloaded the project and played the test scene...The car is steering itself :( How can i prevent it from steering itself? Would be very nice if you help me :)

    Thx

    Edit: Sorry for my bad english
     
  38. njrk97

    njrk97

    Joined:
    Oct 14, 2012
    Posts:
    1
    I have followed the start of your tutorial (first post) and for some reason my wheel colliders are not colliding with the ground and it also does not go and has this error
    IndexOutOfRangeException: Array index is out of range.
    CarController1.GetCollider (Int32 n) (at Assets/CarController1.js:42)
    CarController1.Update () (at Assets/CarController1.js:21)

    Any help?
     
    Last edited: Oct 18, 2012
  39. chandings

    chandings

    Joined:
    Nov 11, 2012
    Posts:
    1
    just drag all 4 wheels from your scene panel to Car's Script>Wheels in the inspector
    just make sure frotleft and frontRight wheels are dragged first.
     
  40. spriggsy

    spriggsy

    Joined:
    Jan 4, 2013
    Posts:
    9
    Hi, not sure if this is still being looked at but.

    im new to unity, mostly used to Obj C and cocos3D but heard great things about unity.

    i have this all working with no probs thanks to krougeau and his fixes.

    the prob is when i import my own heh, it constantly wants to pull wheelies. also this error pops up in the console


    has any one a fix for this?
     
  41. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    that error is usually because a variable was not assigned. You have not "drug" something to something in the inspector, or in code, you are trying to use a variable before it has been assigned. Lastly, this can be caused by removing an object from the game.

    The place where you are refering to has to do with the "WheelController" object. If I remember my code correctly, the only way you can get a wheel onto the car is to put a "WheelController" script on it. At line 325 starts the inclusion of the wheels. So it grabs the WheelController Components and shoves them into the wheels variable.

    If this occurs then it is not possible for you to have a NullReferenceException at 335, since all of the objects have to have the WheelController to start. This would mean that one of the elements of wheels is null. Which means that it has been removed.
     
  42. Mantra-Games

    Mantra-Games

    Joined:
    Nov 27, 2012
    Posts:
    175
    Thanks a lot for taking the time to put this together! I tried to fix all compile errors but I'm still getting:

    Assets/VehicleController/EnvironmentController.js(364,34): BCE0005: Unknown identifier: 'inObject'.
    Assets/VehicleController/EnvironmentController.js(365,17): BCE0022: Cannot convert 'UnityEngine.Component' to 'UnityEngine.GameObject'.

    This is in Unity 4.01.

    Do you offer your other demos, like your 'Fun With Ships' for download?

    Cheers!
     
  43. Saadii

    Saadii

    Joined:
    May 11, 2013
    Posts:
    2




    Car Keeps on Flying when try to drive the car please help whats the problem here???
     
  44. arulraj

    arulraj

    Joined:
    May 13, 2013
    Posts:
    1
    The above Carcontroller shows
    Index out of range exception: array index is out of range

    how to overcome this error
     
  45. arsalany

    arsalany

    Joined:
    Jul 29, 2013
    Posts:
    2
    A very great effort indeed. On your demo page i have noticed that you have 2 demo related to tanks. Any plans to share some information on Tanks. your efforts are much appriciated.
     
  46. rangapraveen

    rangapraveen

    Joined:
    Jun 14, 2013
    Posts:
    1
    i can't move my car , the wheels are half stuck in to the box(floor)...need help with the first tutorial.
     
    Last edited: Aug 1, 2013
  47. Cameronboo

    Cameronboo

    Joined:
    Aug 9, 2012
    Posts:
    7
    Hello everybody

    I have a question. How can we make only aI car with out player car switch. I am trying to make only AI script with this script but it is really complicated for me as a new bee.

    Thanks
     
  48. terbilatorius

    terbilatorius

    Joined:
    Sep 14, 2013
    Posts:
    1
    same here
     
  49. shakey

    shakey

    Joined:
    May 6, 2013
    Posts:
    1
    I'm getting an error from the very first code sample: seems that the wheels variable declared on line 2 is not allocated anywhere. Therefore, I am getting an error on line 42, when a call to wheels[n] fails within GetCollider().

    Any suggestions?

    Thanks!

    Code (csharp):
    1.  
    2. //CarController1.js
    3. var wheels : Transform[];
    4.  
    5. var enginePower=150.0;
    6.  
    7. var power=0.0;
    8. var brake=0.0;
    9. var steer=0.0;
    10.  
    11. var maxSteer=25.0;
    12.  
    13. function Start(){
    14.     rigidbody.centerOfMass=Vector3(0,-0.5,0.3);
    15. }
    16.  
    17. function Update () {
    18.     power=Input.GetAxis("Vertical") * enginePower * Time.deltaTime * 250.0;
    19.     steer=Input.GetAxis("Horizontal") * maxSteer;
    20.     brake=Input.GetKey("space") ? rigidbody.mass * 0.1: 0.0;
    21.    
    22.     GetCollider(0).steerAngle=steer;
    23.     GetCollider(1).steerAngle=steer;
    24.    
    25.     if(brake > 0.0){
    26.         GetCollider(0).brakeTorque=brake;
    27.         GetCollider(1).brakeTorque=brake;
    28.         GetCollider(2).brakeTorque=brake;
    29.         GetCollider(3).brakeTorque=brake;
    30.         GetCollider(2).motorTorque=0.0;
    31.         GetCollider(3).motorTorque=0.0;
    32.     } else {
    33.         GetCollider(0).brakeTorque=0;
    34.         GetCollider(1).brakeTorque=0;
    35.         GetCollider(2).brakeTorque=0;
    36.         GetCollider(3).brakeTorque=0;
    37.         GetCollider(2).motorTorque=power;
    38.         GetCollider(3).motorTorque=power;
    39.     }
    40. }
    41.  
    42. function GetCollider(n : int) : WheelCollider{
    43.     return wheels[n].gameObject.GetComponent(WheelCollider);
    44. }
    45.  
     
  50. Temo2Ginal

    Temo2Ginal

    Joined:
    Oct 29, 2013
    Posts:
    13
    Hi everyone
    I wonder how to write GetCollider(0) in C#
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CarController01 : MonoBehaviour {
    5.     public float enginePower = 150.0f;
    6.     public float power = 0.0f;
    7.     public float brake = 0.0f;
    8.     public float steer = 0.0f;
    9.     public float maxSteer = 25.0f;
    10.     public WheelCollider FrontLeft;
    11.     public WheelCollider FrontRight;
    12.     public WheelCollider RearLeft;
    13.     public WheelCollider RearRight;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         rigidbody.centerOfMass= new Vector3(0,-0.5f,0.3f);
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         power = Input.GetAxis ("Vertical") * enginePower * Time.deltaTime * 250.0f;
    23.         steer = Input.GetAxis ("Horizontal") * maxSteer;
    24.         if (Input.GetKey (KeyCode.Space)) {
    25.                         brake = rigidbody.mass * 0.1f;
    26.                 } else
    27.                         brake = 0.0f;
    28.         FrontLeft.steerAngle = steer;
    29.         FrontRight.steerAngle = steer;
    30.  
    31.         if (brake > 0.0f) {
    32.                         FrontLeft.brakeTorque = brake;
    33.                         FrontRight.brakeTorque = brake;
    34.                         RearLeft.brakeTorque = brake;
    35.                         RearRight.brakeTorque = brake;
    36.                         RearLeft.motorTorque = 0.0f;
    37.                         RearRight.motorTorque = 0.0f;
    38.                 } /*else {
    39.                         FrontLeft.brakeTorque = 0.0f;
    40.                         FrontRight.brakeTorque = 0.0f;
    41.                         RearLeft.brakeTorque = 0.0f;
    42.                         RearRight.brakeTorque = 0.0f;
    43.                         RearLeft.motorTorque = power;
    44.                         RearRight.motorTorque = power;
    45.         }*/
    46.        
    47.        
    48.     }
    49. }
    50.  
    It's not working...
     
    Last edited: Feb 13, 2014