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

Advance Car Game Tutorials Unity 3D

Discussion in 'Community Learning & Teaching' started by FlatTutorials, Nov 11, 2012.

  1. FlatTutorials

    FlatTutorials

    Joined:
    Nov 11, 2012
    Posts:
    1
    Flat Tutorials



    The Car Series

    I can't add all the videos over Here .... Because of the forum rules ...... Here is all of my videos CLICK HERE


    Code :

    Here is the CarControlScript up to Episode #20 :


    Code (csharp):
    1. #pragma strict
    2. var centerOfMass : Vector3;
    3. var wheelFL : WheelCollider;
    4. var wheelFR : WheelCollider;
    5. var wheelRL : WheelCollider;
    6. var wheelRR : WheelCollider;
    7. var wheelFLTrans : Transform;
    8. var wheelFRTrans : Transform;
    9. var wheelRLTrans : Transform;
    10. var wheelRRTrans : Transform;
    11. var lowestSteerAtSpeed : float = 50;
    12. var lowSpeedSteerAngel : float = 10;
    13. var highSpeedSteerAngel : float = 1;
    14. var decellarationSpeed : float = 30;
    15. var maxTorque : float  = 50;
    16. var currentSpeed : float;
    17. var topSpeed : float = 150;
    18. var maxReverseSpeed : float = 50;
    19. var backLightObject : GameObject;
    20. var idleLightMaterial : Material;
    21. var brakeLightMaterial : Material;
    22. var reverseLightMaterial : Material;
    23. private var braked : boolean = false;
    24. var maxBrakeTorque : float = 100;
    25. private var mySidewayFriction : float;
    26. private var myForwardFriction : float;
    27. private var slipSidewayFriction : float;
    28. private var slipForwardFriction : float;
    29. var speedOMeterDial : Texture2D;
    30. var speedOMeterPointer : Texture2D;
    31.  
    32. var gearRatio : int[];
    33. function Start () {
    34. rigidbody.centerOfMass=centerOfMass;
    35. SetValues();
    36. }
    37. function SetValues (){
    38. myForwardFriction  = wheelRR.forwardFriction.stiffness;
    39. mySidewayFriction  = wheelRR.sidewaysFriction.stiffness;
    40. slipForwardFriction = 0.05;
    41. slipSidewayFriction = 0.085;
    42. }
    43.  
    44. function FixedUpdate () {
    45. Controle ();
    46. HandBrake();
    47. }
    48. function Update(){
    49. wheelFLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
    50. wheelFRTrans.Rotate(wheelFR.rpm/60*360*Time.deltaTime,0,0);
    51. wheelRLTrans.Rotate(wheelRL.rpm/60*360*Time.deltaTime,0,0);
    52. wheelRRTrans.Rotate(wheelRR.rpm/60*360*Time.deltaTime,0,0);
    53. wheelFLTrans.localEulerAngles.y = wheelFL.steerAngle - wheelFLTrans.localEulerAngles.z;
    54. wheelFRTrans.localEulerAngles.y = wheelFR.steerAngle - wheelFRTrans.localEulerAngles.z;
    55. BackLight ();
    56. WheelPosition();
    57. ReverseSlip();
    58. EngineSound();
    59. }
    60. function Controle (){
    61. currentSpeed = 2*22/7*wheelRL.radius*wheelRL.rpm*60/1000;
    62. currentSpeed = Mathf.Round(currentSpeed);
    63. if (currentSpeed < topSpeed  currentSpeed > -maxReverseSpeed  !braked){
    64. wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    65. wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    66. }
    67. else {
    68. wheelRR.motorTorque =0;
    69. wheelRL.motorTorque =0;
    70. }
    71. if (Input.GetButton("Vertical")==false){
    72. wheelRR.brakeTorque = decellarationSpeed;
    73. wheelRL.brakeTorque = decellarationSpeed;
    74. }
    75. else{
    76. wheelRR.brakeTorque = 0;
    77. wheelRL.brakeTorque = 0;
    78. }
    79. var speedFactor = rigidbody.velocity.magnitude/lowestSteerAtSpeed;
    80. var currentSteerAngel = Mathf.Lerp(lowSpeedSteerAngel,highSpeedSteerAngel,speedFactor);
    81. currentSteerAngel *= Input.GetAxis("Horizontal");
    82. wheelFL.steerAngle = currentSteerAngel;
    83. wheelFR.steerAngle = currentSteerAngel;
    84. }
    85. function BackLight (){
    86. if (currentSpeed > 0  Input.GetAxis("Vertical")<0!braked){
    87. backLightObject.renderer.material = brakeLightMaterial;
    88. }
    89. else if (currentSpeed < 0  Input.GetAxis("Vertical")>0!braked){
    90. backLightObject.renderer.material = brakeLightMaterial;
    91. }
    92. else if (currentSpeed < 0  Input.GetAxis("Vertical")<0!braked){
    93. backLightObject.renderer.material = reverseLightMaterial;
    94. }
    95. else if (!braked){
    96. backLightObject.renderer.material = idleLightMaterial;
    97. }
    98. }
    99. function WheelPosition(){
    100. var hit : RaycastHit;
    101. var wheelPos : Vector3;
    102.  
    103. //FL
    104.  
    105. if (Physics.Raycast(wheelFL.transform.position, -wheelFL.transform.up,hit,wheelFL.radius+wheelFL.suspensionDistance) ){
    106. wheelPos = hit.point+wheelFL.transform.up * wheelFL.radius;
    107. }
    108. else {
    109. wheelPos = wheelFL.transform.position -wheelFL.transform.up* wheelFL.suspensionDistance;
    110. }
    111. wheelFLTrans.position = wheelPos;
    112.  
    113. //FR
    114.  
    115. if (Physics.Raycast(wheelFR.transform.position, -wheelFR.transform.up,hit,wheelFR.radius+wheelFR.suspensionDistance) ){
    116. wheelPos = hit.point+wheelFR.transform.up * wheelFR.radius;
    117. }
    118. else {
    119. wheelPos = wheelFR.transform.position -wheelFR.transform.up* wheelFR.suspensionDistance;
    120. }
    121. wheelFRTrans.position = wheelPos;
    122.  
    123. //RL
    124.  
    125. if (Physics.Raycast(wheelRL.transform.position, -wheelRL.transform.up,hit,wheelRL.radius+wheelRL.suspensionDistance) ){
    126. wheelPos = hit.point+wheelRL.transform.up * wheelRL.radius;
    127. }
    128. else {
    129. wheelPos = wheelRL.transform.position -wheelRL.transform.up* wheelRL.suspensionDistance;
    130. }
    131. wheelRLTrans.position = wheelPos;
    132.  
    133. //RR
    134.  
    135. if (Physics.Raycast(wheelRR.transform.position, -wheelRR.transform.up,hit,wheelRR.radius+wheelRR.suspensionDistance) ){
    136. wheelPos = hit.point+wheelRR.transform.up * wheelRR.radius;
    137. }
    138. else {
    139. wheelPos = wheelRR.transform.position -wheelRR.transform.up* wheelRR.suspensionDistance;
    140. }
    141. wheelRRTrans.position = wheelPos;
    142. }
    143. function HandBrake(){
    144. if (Input.GetButton("Jump")){
    145. braked = true;
    146. }
    147. else{
    148. braked = false;
    149. }
    150. if (braked){
    151. if (currentSpeed > 1){
    152. wheelFR.brakeTorque = maxBrakeTorque;
    153. wheelFL.brakeTorque = maxBrakeTorque;
    154. wheelRR.motorTorque =0;
    155. wheelRL.motorTorque =0;
    156. SetRearSlip(slipForwardFriction ,slipSidewayFriction);
    157. }
    158. else if (currentSpeed < 0){
    159. wheelRR.brakeTorque = maxBrakeTorque;
    160. wheelRL.brakeTorque = maxBrakeTorque;
    161. wheelRR.motorTorque =0;
    162. wheelRL.motorTorque =0;
    163. SetRearSlip(1 ,1);
    164. }
    165. else {
    166. SetRearSlip(1 ,1);
    167. }
    168. if (currentSpeed < 1  currentSpeed > -1){
    169. backLightObject.renderer.material = idleLightMaterial;
    170. }
    171. else {
    172. backLightObject.renderer.material = brakeLightMaterial;
    173. }
    174. }
    175. else {
    176. wheelFR.brakeTorque = 0;
    177. wheelFL.brakeTorque = 0;
    178. SetRearSlip(myForwardFriction ,mySidewayFriction);
    179. }
    180. }
    181. function ReverseSlip(){
    182. if (currentSpeed <0){
    183. SetFrontSlip(slipForwardFriction ,slipSidewayFriction);
    184. }
    185. else {
    186. SetFrontSlip(myForwardFriction ,mySidewayFriction);
    187. }
    188. }
    189.  
    190. function SetRearSlip (currentForwardFriction : float,currentSidewayFriction : float){
    191. wheelRR.forwardFriction.stiffness = currentForwardFriction;
    192. wheelRL.forwardFriction.stiffness = currentForwardFriction;
    193. wheelRR.sidewaysFriction.stiffness = currentSidewayFriction;
    194. wheelRL.sidewaysFriction.stiffness = currentSidewayFriction;
    195. }
    196. function SetFrontSlip (currentForwardFriction : float,currentSidewayFriction : float){
    197. wheelFR.forwardFriction.stiffness = currentForwardFriction;
    198. wheelFL.forwardFriction.stiffness = currentForwardFriction;
    199. wheelFR.sidewaysFriction.stiffness = currentSidewayFriction;
    200. wheelFL.sidewaysFriction.stiffness = currentSidewayFriction;
    201. }
    202. function EngineSound(){
    203. for (var i = 0; i < gearRatio.length; i++){
    204. if(gearRatio[i]> currentSpeed){
    205. break;
    206. }
    207. }
    208. var gearMinValue : float = 0.00;
    209. var gearMaxValue : float = 0.00;
    210. if (i == 0){
    211. gearMinValue = 0;
    212. }
    213. else {
    214. gearMinValue = gearRatio[i-1];
    215. }
    216. gearMaxValue = gearRatio[i];
    217. var enginePitch : float = ((currentSpeed - gearMinValue)/(gearMaxValue - gearMinValue))+1;
    218. audio.pitch = enginePitch;
    219. }
    220.  
    221. function OnGUI (){
    222. GUI.DrawTexture(Rect(Screen.width - 300,Screen.height-150,300,150),speedOMeterDial);
    223. var speedFactor : float = currentSpeed / topSpeed;
    224. var rotationAngle : float;
    225. if (currentSpeed >= 0){
    226.   rotationAngle = Mathf.Lerp(0,180,speedFactor);
    227.   }
    228.   else {
    229.   rotationAngle = Mathf.Lerp(0,180,-speedFactor);
    230.   }
    231. GUIUtility.RotateAroundPivot(rotationAngle,Vector2(Screen.width-150,Screen.height));
    232. GUI.DrawTexture(Rect(Screen.width - 300,Screen.height-150,300,300),speedOMeterPointer);
    233.  
    234. }

    Here is the CarCameraScript :


    Code (csharp):
    1. #pragma strict
    2. var car : Transform;
    3. var distance : float = 6.4;
    4. var height : float = 1.4;
    5. var rotationDamping : float = 3.0;
    6. var heightDamping : float = 2.0;
    7. var zoomRacio : float = 0.5;
    8. var DefaultFOV : float = 60;
    9. private var rotationVector : Vector3;
    10. function Start () {
    11. }
    12.  
    13. function LateUpdate () {
    14. var wantedAngel = rotationVector.y;
    15. var wantedHeight = car.position.y + height;
    16. var myAngel = transform.eulerAngles.y;
    17. var myHeight = transform.position.y;
    18. myAngel = Mathf.LerpAngle(myAngel,wantedAngel,rotationDamping*Time.deltaTime);
    19. myHeight = Mathf.Lerp(myHeight,wantedHeight,heightDamping*Time.deltaTime);
    20. var currentRotation = Quaternion.Euler(0,myAngel,0);
    21. transform.position = car.position;
    22. transform.position -= currentRotation*Vector3.forward*distance;
    23. transform.position.y = myHeight;
    24. transform.LookAt(car);
    25. }
    26. function FixedUpdate (){
    27. var localVilocity = car.InverseTransformDirection(car.rigidbody.velocity);
    28. if (localVilocity.z<-0.5){
    29. rotationVector.y = car.eulerAngles.y + 180;
    30. }
    31. else {
    32. rotationVector.y = car.eulerAngles.y;
    33. }
    34. var acc = car.rigidbody.velocity.magnitude;
    35. camera.fieldOfView = DefaultFOV + acc*zoomRacio;
    36. }
    37.  

    Here is the SkiddingScript :


    Code (csharp):
    1. #pragma strict
    2. private var currentFrictionValue : float;
    3. var skidAt : float = 1.5;
    4. var soundEmition : float = 15;
    5. private var soundWait : float;
    6. var skidSound : GameObject;
    7. var skidSmoke : GameObject;
    8. var smokeDepth : float = 0.4;
    9. var markWidth : float = 0.2;
    10. var rearWheel : boolean;
    11. private var skidding : int;
    12. private var lastPos = new Vector3[2];
    13. var skidMaterial : Material;
    14. function Start () {
    15. skidSmoke.transform.position = transform.position;
    16. skidSmoke.transform.position.y -= smokeDepth;
    17. }
    18.  
    19. function Update () {
    20. var hit : WheelHit;
    21. transform.GetComponent(WheelCollider).GetGroundHit(hit);
    22. currentFrictionValue = Mathf.Abs(hit.sidewaysSlip);
    23. var rpm = transform.GetComponent(WheelCollider).rpm;
    24. if (skidAt <= currentFrictionValue  soundWait <= 0 || rpm < 300  Input.GetAxis("Vertical")>0  soundWait <= 0  rearWheel  hit.collider){
    25. Instantiate(skidSound,hit.point,Quaternion.identity);
    26. soundWait = 1;
    27. }
    28. soundWait -= Time.deltaTime*soundEmition;
    29. if (skidAt <= currentFrictionValue || rpm < 300  Input.GetAxis("Vertical")>0  rearWheel  hit.collider){
    30. skidSmoke.particleEmitter.emit = true;
    31. SkidMesh();
    32. }
    33. else {
    34. skidSmoke.particleEmitter.emit = false;
    35. skidding = 0;
    36. }
    37. }
    38.  
    39. function SkidMesh(){
    40.  
    41. var hit : WheelHit;
    42. transform.GetComponent(WheelCollider).GetGroundHit(hit);
    43. var mark : GameObject = new GameObject("Mark");
    44. var filter : MeshFilter = mark.AddComponent(MeshFilter);
    45. mark.AddComponent(MeshRenderer);
    46. var markMesh : Mesh = new Mesh();
    47. var vertices = new Vector3 [4];
    48. var triangles = new int[6];
    49.  
    50. if (skidding == 0){
    51. vertices[0] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(markWidth,0.01,0);
    52. vertices[1] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(-markWidth,0.01,0);
    53. vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(-markWidth,0.01,0);
    54. vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(markWidth,0.01,0);
    55. lastPos[0] = vertices[2];
    56. lastPos[1] = vertices[3];
    57. skidding = 1;
    58. }
    59. else {
    60. vertices[1] = lastPos[0];
    61. vertices[0] = lastPos[1];
    62. vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(-markWidth,0.01,0);
    63. vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z)*Vector3(markWidth,0.01,0);
    64. lastPos[0] = vertices[2];
    65. lastPos[1] = vertices[3];
    66. }
    67.  
    68. triangles = [0,1,2,2,3,0];
    69. markMesh.vertices = vertices;
    70. markMesh.triangles = triangles;
    71. markMesh.RecalculateNormals();
    72. var uvm: Vector2[] = new Vector2[4];
    73. uvm[0] = Vector2(1,0);
    74. uvm[1] = Vector2(0,0);
    75. uvm[2] = Vector2(0,1);
    76. uvm[3] = Vector2(1,1);
    77. markMesh.uv = uvm;
    78. filter.mesh = markMesh;
    79. mark.renderer.material = skidMaterial;
    80. mark.AddComponent(DestroyTimerScript);
    81. }

    I am Not Really Active On this forum ..... But You can ask about your problem on facebook


    If you like to get notified about my uploads on youtube then be sure to SUBSCRIBE me on youtube
     
    Last edited: Jun 14, 2013
    jebi likes this.
  2. johan@blafilm.se

    johan@blafilm.se

    Joined:
    Dec 22, 2012
    Posts:
    1
    Ive just got one problem that my car flipping around when im driving and stering. Then is rears when get full throttle like a bike :( Try to fix it but it dousent work at all
     
    med zeg likes this.
  3. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Awsome !!!! Bookmarked :D
     
    Last edited: Jan 7, 2013
  4. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    I watched all your tutorial and its really very nice. Keep it up.

    Thanks.
     
  5. arkex

    arkex

    Joined:
    Jan 24, 2013
    Posts:
    1
    hi, i have a problem!

    why when i have adding the final script my wheels are crazy?:confused:
     
  6. LEO_DARLING

    LEO_DARLING

    Joined:
    Dec 13, 2012
    Posts:
    1
    Camera LookScript problem

    var car : Transform;
    var distance : float = 6.4;
    var height : float = 1.4;
    var rotationDamping : float = 3.0;
    var heightDamping : float = 2.0;
    var zoomRacio : float = 0.5;
    private var rotationVector : Vector3;

    function Update () {
    var wantedAngle = car.eulerAngles.y;
    var wantedHeight = car.position.y + height;
    var myAngle = transform.eulerAngles.y;
    var myHeight = transform.position.y;
    myAngle = Mathf.Lerp(myAngle , wantedAngle , rotationDamping * Time.deltaTime);
    myHeight = Mathf.Lerp(myHeight , wantedHeight , heightDamping * Time.deltaTime);
    var currentRotation = Quaternion.Euler(0 , myAngle , 0);
    transform.position = car.position;
    transform.position -= currentRotation * Vector3.forward * distance;
    transform.position.y = myHeight;
    transform.LookAt(car);
    }
     
  7. bio

    bio

    Joined:
    Mar 14, 2013
    Posts:
    1
    i got same problem with the wheel from episode #5, it rotate like the windmill. seem the point of rotation is not at center of the object. if we change from the Unity GUI for x axis rotate only for a random value, it have the same problem. i believe the root cause of this is because of the wheel mesh imported. Anyone know how to fix this?
     
    Last edited: Mar 14, 2013
    wakatsuki83 likes this.
  8. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    I have not read all through so I may be wrong...

    I would guess you used a cylinder for your wheel that you rotate to get the flat parts vertically.
    But I would also guess the script is rotating the wheel model on the x axis the horizontal one which is now vertical for you since you rotate.

    His goes fine probably because he is using a model made on a modeling software which allows you to model properly from the start.

    Is that so?

    But I am having another issue with the code:

    if (currentSpeed < topSpeed currentSpeed > -maxReverseSpeed !braked){
    wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    }else {
    wheelRR.motorTorque =0;
    wheelRL.motorTorque =0;
    }

    if (Input.GetButton("Vertical")==false){
    wheelRR.brakeTorque = decellarationSpeed;
    wheelRL.brakeTorque = decellarationSpeed;
    }else{
    wheelRR.brakeTorque = 0;
    wheelRL.brakeTorque = 0;
    }

    Apart from those lines, I do not see where the brake is applied. It seems the negative value of the GetAxis is used to apply an opposite force to the motorTorque which according to most users and Unity docs is to be avoided. http://docs.unity3d.com/Documentation/ScriptReference/WheelCollider-motorTorque.html

    Am I missing a part?
     
    Last edited: Mar 14, 2013
  9. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Awesome sauce, I need to move this to teaching though.
     
  10. mahmoud.italy

    mahmoud.italy

    Joined:
    Mar 19, 2013
    Posts:
    2
    i have same problem like arkex my wheel is sping high need help
     
  11. mahmoud.italy

    mahmoud.italy

    Joined:
    Mar 19, 2013
    Posts:
    2
    and i watched the movies over and over and i guss i didn't mistake need realy help this my project for collage :(
     
  12. wanglang3081

    wanglang3081

    Joined:
    Aug 1, 2013
    Posts:
    1
    very good tutorial, But the model can not be downloaded now.
     
  13. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You're responding to a thread of a person who dropped a single post on this forum, said he'd be on Facebook, and never came back.
     
  14. rameshkumar

    rameshkumar

    Joined:
    Aug 5, 2013
    Posts:
    50
    hai dude when will you start the AI serious? waiting for that eagerly..;)
     
  15. chrisMary

    chrisMary

    Joined:
    Nov 27, 2013
    Posts:
    16
    The flipping is probably due to the position of the centerOfMass... I usually create an empty game object that i move around and use its position as centerOfMass. It has to be quite low and maybe a bit forward
     
  16. ashleywells

    ashleywells

    Joined:
    Nov 26, 2013
    Posts:
    4
    Thank you for sharing this code. I will try this at home. I'm done watching all tutorials you have shared and its very cool and interesting.
     
  17. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Hi, this is a very nice script! BUT I have tried to use it for a Bike setup but am struggling a bit, have you got any ideas or advise on how to convert this for a bike?
     
  18. nzee

    nzee

    Joined:
    May 5, 2014
    Posts:
    3
    i hve also problem in wheel transform
     
  19. nzee

    nzee

    Joined:
    May 5, 2014
    Posts:
    3
    in bike
     
  20. DevProNum1

    DevProNum1

    Joined:
    Apr 25, 2014
    Posts:
    2
    @Cxlaced I want it! Post it please. Thanks!
     
  21. DevProNum1

    DevProNum1

    Joined:
    Apr 25, 2014
    Posts:
    2
    Yes I want it in c# post it please!
     
  22. Siyana

    Siyana

    Joined:
    Jan 6, 2014
    Posts:
    1
    I want to see C# code too. Thank you very much. :)
     
  23. sivaji

    sivaji

    Joined:
    Jun 4, 2014
    Posts:
    3
    I do want c# script please post it thanks in advance
     
    Munaam likes this.
  24. sivaji

    sivaji

    Joined:
    Jun 4, 2014
    Posts:
    3
    Thanks it is more helpful can you please provide unity source file for this race thanks
     
  25. Deleted User

    Deleted User

    Guest

    i got the camera script i C# if someone want it too :) PM me.
     
    stijflijk likes this.
  26. marcell123455

    marcell123455

    Joined:
    Jun 18, 2014
    Posts:
    274
    Can you make a tutorial were you explane how to create a skript were you can tune your car in garage? Also that you can change parts of your car bumpers, wheels ,spoiler and more . Not only colour
     
  27. stijflijk

    stijflijk

    Joined:
    Jul 30, 2014
    Posts:
    2
    Hi, Could you please share the camera script in C#? I would really appreciate it, thank you.
     
  28. Ferdi18

    Ferdi18

    Joined:
    Jul 19, 2014
    Posts:
    13
    For now I just want to say thanks for the youtube videos, I'm learning a bunch. Also, an extra thank you to Cxlaced for sharing a C# version of the script, because I got completely stuck at Episode #6 (steering) until I saw your version of the script!
     
  29. Ferdi18

    Ferdi18

    Joined:
    Jul 19, 2014
    Posts:
    13
    Regarding the C# conversion by cxlaced.

    I'm trying to get the handbrake system to work. But it seems that though variables are changed, they are never used to change the forward and sideways friction stifness.

    In short I don't see a wheelRR.forwardFriction.stiffness on the left side of the = sign. Because it is probably a struct this will be not as simple as wheelRR.forwardFriction.stiffness = slipSidewaysFriction;

    Am I mistaken?

    found the solution at http://forum.unity3d.com/threads/wheelcolliders-stiffness-and-c.16032/

    Create variables of the type WheelFrictionCurve
    changes its values to the wheelcolliders forwardFriction and sidewaysFriction
    change yourvariable.stiffness to another value
    finally, change yourWheelCollider.forwardFriction = your WheelFrictionCurve variable

    I will change all this to something more legible when my unity PC and internet PC are one and the same machine again.
     
    Last edited: Sep 5, 2014
  30. GamingMaster474

    GamingMaster474

    Joined:
    Mar 26, 2015
    Posts:
    1
    Your code is so messed up!
    I don't know whether it is C# or Javascript.
    It is getting too many errors!
     
  31. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    It's Javascript. Which errors are you getting?
     
  32. Hrnjak

    Hrnjak

    Joined:
    Jun 4, 2015
    Posts:
    1
    Thank you for sharing this code !!!!! Thank you !!!!! ;):):D
     
  33. assiri11

    assiri11

    Joined:
    Dec 2, 2015
    Posts:
    1
  34. tsj

    tsj

    Joined:
    Jul 9, 2014
    Posts:
    1
    When I try to use the script the wheels of the car detact from the car and fly above the car, they DO turn and rotate... but nomatter what I am unable to make them "stick" to the car once the script is applied ??? I tried like 20 times now.. each time following your tutorial to the letter... but with minor variations, such as how and where wheel colliders and wheels transforms are placed "in" the car object, how they are rotatated and many more things... nothing works... every attempt makes the wheels misbehave and all attempts make the wheels be somehwere else than on the car... but ONLY when using the car control script
     
  35. FaisalFarooq

    FaisalFarooq

    Joined:
    Mar 12, 2016
    Posts:
    1
    Thanks alot . But the problem is that the wheel does not rotate even after coding it 100 % right . Please help
     
  36. kownjaehyun

    kownjaehyun

    Joined:
    Jul 13, 2015
    Posts:
    5
    it is C# Code.
    may there has some add code and changed code anyway it is work well ~

    void SkidMesh()
    {
    WheelHit hit;
    transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
    //생성
    GameObject mark = new GameObject("Mark");
    MeshFilter filter = mark.AddComponent<MeshFilter>();
    mark.AddComponent<MeshRenderer>();
    Mesh markMesh = new Mesh();
    var vertices = new Vector3[4];
    var triangles = new int[6];

    //바퀴 UV 좌표 그리기
    if (_skidding == 0)
    {
    vertices[0] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(_markWidth, 0.01f, 0.5f);
    vertices[1] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-_markWidth, 0.01f, 0.5f);
    vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-_markWidth, 0.01f, 0.5f);
    vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(_markWidth, 0.01f, 0.5f);
    _lastPos[0] = vertices[2];
    _lastPos[1] = vertices[3];
    _skidding = 1;
    }
    else
    {
    vertices[1] = _lastPos[0];
    vertices[0] = _lastPos[1];
    vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-_markWidth, 0.01f, 0.5f);
    vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(_markWidth, 0.01f, 0.5f);
    _lastPos[0] = vertices[2];
    _lastPos[1] = vertices[3];
    }


    triangles = new int[] { 0, 1, 2, 2, 3, 0 };
    markMesh.vertices = vertices;
    markMesh.triangles = triangles;
    //markMesh.RecalculateNormals();
    Vector2[] uvm = new Vector2[4];
    uvm[0] = new Vector2(1, 0);
    uvm[1] = new Vector2(0, 0);
    uvm[2] = new Vector2(0, 1);
    uvm[3] = new Vector2(1, 1);
    markMesh.uv = uvm;
    //markMesh = skidMeshFilter.mesh;
    filter.mesh = markMesh;
    mark.GetComponent<Renderer>().material = _skidMaterial;
    mark.transform.parent = GameObject.Find("SkidMark").transform;
    }
    #endregion
    }
     
  37. hanzala717

    hanzala717

    Joined:
    Sep 27, 2013
    Posts:
    2
    There is a problem will you help me?
     
  38. faresdevunity

    faresdevunity

    Joined:
    Aug 28, 2017
    Posts:
    1
    thank you but i have a problem it is in car sound #15 the script cannot run what i do ??? please help me i need a answer
     
  39. Halishmakhan

    Halishmakhan

    Joined:
    Sep 24, 2017
    Posts:
    1
    Hi plz help me
    I have problem in episode no 4
     
  40. ISTA33

    ISTA33

    Joined:
    Apr 2, 2020
    Posts:
    8
    we are in 2021/09/02 this script is still the best script at all in the car controller
    will after updating somethings like
    var = float
    and functiont = void
    and adding "&&" this in the if statement bitween (word 1 < < word 2 && !braked) for example
    thank you very mutch sir !!!