Search Unity

Simple vehicle script?

Discussion in 'Scripting' started by jeffmorris, Nov 17, 2011.

Thread Status:
Not open for further replies.
  1. jeffmorris

    jeffmorris

    Joined:
    Nov 26, 2009
    Posts:
    144
    I created a simple car driving game to see if I can get mirrors working in free version of Unity 3D and I got them working. For now, I made the car move forward and backwards without using physics. Is there a simple vehicle script that use physics? Is it possible to have the car's doors, hood, and trunk lid open and close? I used the mirror script from http://www.unifycommunity.com/wiki/index.php?title=InvertCamera
     

    Attached Files:

    trevour1738 likes this.
  2. jeffmorris

    jeffmorris

    Joined:
    Nov 26, 2009
    Posts:
    144
    http://forum.unity3d.com/threads/86...icle-Tutorial?highlight=advanced+car+tutorial
    http://forum.unity3d.com/threads/104629-How-to-convert-float-to-string?highlight=float+string

    I finally created a simple driving game by modifying the code from the car tutorial in the first link.

    Code (csharp):
    1.  
    2. //CarController1.js
    3. var wheels : Transform[];
    4. var enginePower=50.0;
    5. var power=0.0;
    6. var brake=0.0;
    7. var steer=0.0;
    8. var maxSteer=25.0;
    9. var forwardgear=false;
    10. var reversegear=false;
    11. var keypressed=0;
    12. var gearstring = "Neutral";
    13. function Start(){
    14.     rigidbody.centerOfMass=Vector3(0,-0.5,0.3);
    15. }
    16.  
    17. function Update ()
    18. {
    19.     if (Input.GetKeyDown("a"))
    20.     {
    21.       keypressed = keypressed + 1;
    22.       if (keypressed >= 1)
    23.         keypressed = 1;
    24.     }
    25.     if (Input.GetKeyDown("z"))
    26.     {
    27.       keypressed = keypressed - 1;
    28.       if (keypressed <= -1)
    29.         keypressed = -1;
    30.     }
    31.     if (keypressed == -1)
    32.     {
    33.       forwardgear = false;
    34.       reversegear = true;
    35.       gearstring = "Reverse";
    36.     }  
    37.     if (keypressed == 0)
    38.     {
    39.       forwardgear = false;
    40.       reversegear = false;
    41.       gearstring = "Neutral";
    42.     }  
    43.     if (keypressed == 1)
    44.     {
    45.       forwardgear = true;
    46.       reversegear = false;
    47.       gearstring = "Forward";
    48.     }  
    49.     if (forwardgear)
    50.     {
    51.         power = Input.GetAxis("throttle");
    52.         power = (power + 1) * 0.5;
    53.         power = power * enginePower * Time.deltaTime * 100.0;
    54.         steer=Input.GetAxis("steering") * maxSteer;
    55.         brake=Input.GetAxis("brakes");
    56.         brake = (brake +1) * 0.5;
    57.         brake = brake ? rigidbody.mass * 0.05: 0.0;
    58.         GetCollider(0).steerAngle=steer;
    59.         GetCollider(1).steerAngle=steer;
    60.         if(brake > 0.0)
    61.         {
    62.             GetCollider(0).brakeTorque=brake;
    63.             GetCollider(1).brakeTorque=brake;
    64.             GetCollider(2).brakeTorque=brake;
    65.             GetCollider(3).brakeTorque=brake;
    66.             GetCollider(2).motorTorque=0.0;
    67.             GetCollider(3).motorTorque=0.0;
    68.         } else {
    69.             GetCollider(0).brakeTorque=0;
    70.             GetCollider(1).brakeTorque=0;
    71.             GetCollider(2).brakeTorque=0;
    72.             GetCollider(3).brakeTorque=0;
    73.             GetCollider(2).motorTorque=power;
    74.             GetCollider(3).motorTorque=power;
    75.         }
    76.     }
    77.     if (reversegear)
    78.     {
    79.         power = Input.GetAxis("throttle");
    80.         power = (power + 1) * 0.5;
    81.         power = -power;
    82.         power = power * enginePower * Time.deltaTime * 50.0;
    83.         steer=Input.GetAxis("steering") * maxSteer;
    84.         brake=Input.GetAxis("brakes");
    85.         brake = (brake +1) * 0.5;
    86.         brake = brake ? rigidbody.mass * 0.05: 0.0;
    87.         GetCollider(0).steerAngle=steer;
    88.         GetCollider(1).steerAngle=steer;
    89.         if(brake > 0.0)
    90.         {
    91.             GetCollider(0).brakeTorque=brake;
    92.             GetCollider(1).brakeTorque=brake;
    93.             GetCollider(2).brakeTorque=brake;
    94.             GetCollider(3).brakeTorque=brake;
    95.             GetCollider(2).motorTorque=0.0;
    96.             GetCollider(3).motorTorque=0.0;
    97.         } else {
    98.             GetCollider(0).brakeTorque=0;
    99.             GetCollider(1).brakeTorque=0;
    100.             GetCollider(2).brakeTorque=0;
    101.             GetCollider(3).brakeTorque=0;
    102.             GetCollider(2).motorTorque=power;
    103.             GetCollider(3).motorTorque=power;
    104.         }
    105.     }
    106.  
    107. }
    108. function GetCollider(n : int) : WheelCollider{
    109.     return wheels[n].gameObject.GetComponent(WheelCollider);
    110. }
    111. function OnGUI () {
    112. GUI.Label (Rect (10, 10, 100, 20), gearstring);
    113. GUI.Label (Rect (10, 30, 100, 20), power.ToString());
    114. GUI.Label (Rect (10, 50, 100, 20), brake.ToString());
    115. }
    116.  
    I need to convert float to string for GUI.Label code. Will the code in the second link work? It worked.
     
    Last edited: Nov 18, 2011
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    in UnityScript converting a float to a string is as easy as this:

    "" + myString;
     
  4. descenderdante

    descenderdante

    Joined:
    Sep 3, 2009
    Posts:
    218
    for converting float to string you can also use variable.ToString(); works for both UnityScript and C#
     
  5. jeffmorris

    jeffmorris

    Joined:
    Nov 26, 2009
    Posts:
    144
    I used variable.ToString(); and it worked.
     
  6. Andrew_m_aus

    Andrew_m_aus

    Joined:
    Jan 13, 2013
    Posts:
    1
    is there a way to make it if im in range and i press "e" i can drive the car and then if i press "e" again i can get out and walk
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  8. johnunity

    johnunity

    Joined:
    Jul 14, 2013
    Posts:
    1
    got some problem like this:Namespace 'UnityEditor' not found, maybe you forgot to add an assembly reference?(BCE0021)
    ^
    ^
    how to solve this problem? ^
     
  9. Gilly22

    Gilly22

    Joined:
    Oct 23, 2014
    Posts:
    1
    is that unity script? or Java? and where do i change the float to the a string
     
  10. Deleted User

    Deleted User

    Guest

    That is JavaScript, and you can convert the floats in lines 112, 113 and 114 to strings, but they are already converted.
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'd love to know how people end up replying to these old threads..? it's over a year old and the person asking the question you've replied to hasn't logged back in since they posted it. :confused::rolleyes:
     
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    Would you like to know more?
     
  13. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi guys, I'm new to Unity and just tried the JS script by jeffmorris on my spaceship project. Unfortunately I am getting an error "Input Axis throttle is not setup" The console prompts me to Edit - Project Settings - Input, but I have no clue as to how I use the Input Manager...Is there someone who can give a quick answer to this problem?

    Thanks!
     
  14. RepublicCommando

    RepublicCommando

    Joined:
    Jan 8, 2014
    Posts:
    5
    -> Edit.ProjectSettings.Input pulls up an inspector of sorts. You can change or add inputs by:
    Adding to Axis "Size," which will automatically duplicate the last axis for your editing. OR
    Right-clicking an element and clicking "Duplicate Array Element."

    Alternatively, you could just use an input that is already set up. I know "Vertical" is set up to use Up and Down arrow keys; which are typical throttle keys is like games.
     
  15. Felegeunity

    Felegeunity

    Joined:
    Jul 29, 2016
    Posts:
    1
    all complier errors have to be fixed beefore playmode fix please
     
  16. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    hmmm.... resurrection of a 5 year old post. nice.
     
  17. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    It's a thirteen year old thread with UnityScript code. What were you actually expecting?
     
    Chubzdoomer and Kurt-Dekker like this.
Thread Status:
Not open for further replies.