Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help ! Script not working correctly..

Discussion in 'Scripting' started by Droove92, Apr 25, 2014.

  1. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    Hey everyone !
    Now a new problem is really getting me crazy :) :D

    take a look at this script below,.. this is for simple movement of a car according to up and down buttons......
    Earlier it worked perfectly......but now , when I have recently started working on it again, its not working correctly

    Code (csharp):
    1. if(!Input.GetAxis("Vertical"))
    2.     {
    3.         WheelRR.brakeTorque = deacceleration;
    4.         WheelRL.brakeTorque = deacceleration;
    5.         WheelRR.motorTorque = 0;
    6.         WheelRL.motorTorque = 0;
    7.     }
    8.     else
    9.     {
    10.         WheelRR.brakeTorque = 0;
    11.         WheelRL.brakeTorque = 0;
    12.     }
    13.     if(currentSpeed < tpSpeed  currentSpeed > -maxReverseSpeed  !braked  Input.GetAxis("Vertical")) //braked is a boolean for checking if handbrake is applied
    14.     {
    15.        
    16.         WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    17.         WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    18.        
    19.     }
    20.     else
    21.     {
    22.    
    23.         WheelRR.motorTorque = 0;
    24.         WheelRL.motorTorque = 0;
    25.        
    26.     }
    the braking and acceleration are working fine, but when user don't press anything then car should stop automatically (because a "deacceleration" amt. of braketorque is applied)
    it worked earlier, but now it doesn't !! :(
    I did not touch this piece of code at all.. this is as it is I wrote it earlier....

    and the big problem is that, since car doesn't slow down, it also moves at a constant speed (the value with which it was last accelerated)!!
    be it 50, 100, 200 or anything !!

    help me!

    thanks in advance!!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    isn't the "user didn't provide any input this frame" result for Input.GetAxis() 0, not null.... ?
     
  3. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    if you mean this :
    Code (csharp):
    1. if(Input.GetAxis("Vertical")==0)
    2.     {
    3.         apply the brakes.....
    4.     }
    I tried it and it didn't work.....same status.....
     
  4. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    I'm still stuck at this one.....
    don't know what on Earth has happened to the script....
    I even copy- pasted the entire script in a new .js file, then also , no luck! :(
     
  5. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    In that case upload your project.

    There seems to be no error in the script part you have posted, but the error maybe somewhere else too. And also some optimizations can be done to your code.
     
  6. Suddan

    Suddan

    Joined:
    Jan 12, 2013
    Posts:
    21
    Try to debug the deacceleration on the console when running the game.
    It appears to be zero, which can have reasons such as

    - division of int/int i.e. 3/4 = 0, make sure that it's at least float/int = float or int/float = float
    - assigning a value directly and not setting it via methods like awake or start => you might have set it to 0 in inspector which will be used instead of the assigned value
     
  7. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    well.....here is the complete script which handles the movement of car in the scene
    I'm following some tutorials...

    Code (csharp):
    1. #pragma strict
    2.  
    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. private var braked : boolean;
    12. var MoveFlag : countdown;
    13.  
    14. var mySidewayFriction : float;
    15. var myForwardFriction : float;
    16. var slipSidewayFriction : float;
    17. var slipForwardFriction : float;
    18.  
    19. var lowestSteerAtSpeed : float = 50;
    20. var maxReverseSpeed : float = 50;
    21. var currentSpeed : float = 0;
    22. var tpSpeed : float = 150;
    23. var deacceleration : float = 90;
    24. var lowSpeedSteerAngle : float = 15;
    25. var highSpeedSteerAngle : float = 8;
    26. var maxTorque : float  = 50;
    27. var maxBreakTorque : float = 100;
    28. var GearRatio : int [];
    29.  
    30. function Awake()
    31. {
    32.     MoveFlag=FindObjectOfType(countdown);
    33. }
    34.  
    35. function Start ()
    36. {
    37.  
    38.     rigidbody.centerOfMass.y = -0.9;
    39.     rigidbody.centerOfMass.z = 0.5;
    40.     SetValues();
    41.  
    42. }
    43.  
    44. function SetValues()
    45. {
    46.  
    47.     myForwardFriction = WheelRR.forwardFriction.stiffness;
    48.     mySidewayFriction = WheelRR.sidewaysFriction.stiffness;
    49.     slipForwardFriction = 0.04;
    50.     slipSidewayFriction = 0.08;
    51.  
    52. }
    53.  
    54. function FixedUpdate ()
    55. {
    56.     if(MoveFlag.isOver)
    57.     {
    58.         this.rigidbody.isKinematic=false;
    59.         control();
    60.    
    61.         Handbrake();
    62.     }
    63.     else
    64.     this.rigidbody.isKinematic=true;
    65.  
    66. }
    67.  
    68. function Update ()
    69. {
    70.    
    71.     if(MoveFlag.isOver)
    72.     {
    73.         WheelFLTrans.Rotate(WheelFL.rpm/60*360*Time.deltaTime,0,0);
    74.         WheelFRTrans.Rotate(-WheelFR.rpm/60*360*Time.deltaTime,0,0);
    75.         WheelRLTrans.Rotate(WheelRL.rpm/60*360*Time.deltaTime,0,0);
    76.         WheelRRTrans.Rotate(-WheelRR.rpm/60*360*Time.deltaTime,0,0);
    77.         WheelFLTrans.localEulerAngles.y = WheelFL.steerAngle - WheelFLTrans.localEulerAngles.z;
    78.         WheelFRTrans.localEulerAngles.y = WheelFR.steerAngle - WheelFRTrans.localEulerAngles.z+180;
    79.         WheelPosition();
    80.     }
    81.     EngineSound ();
    82.  
    83. }
    84.  
    85. function control ()
    86. {
    87.  
    88.     currentSpeed = 2*3.14*WheelRL.radius*WheelRL.rpm*60/1000;
    89.     currentSpeed = Mathf.Round(currentSpeed);
    90.     Debug.Log("accln nahi diya :"+(!(Input.GetButtonDown("Vertical")==0)));
    91.     if(Input.GetAxis("Vertical")==0)
    92.     {
    93.         WheelRR.brakeTorque = deacceleration;
    94.         WheelRL.brakeTorque = deacceleration;
    95.         WheelRR.motorTorque = 0;
    96.         WheelRL.motorTorque = 0;
    97.     }
    98.     else
    99.     {
    100.         WheelRR.brakeTorque = 0;
    101.         WheelRL.brakeTorque = 0;
    102.     }
    103.     if(currentSpeed < tpSpeed  currentSpeed > -maxReverseSpeed  !braked  Input.GetAxis("Vertical")) //braked is a boolean for checking if handbrake is applied
    104.     {
    105.        
    106.         WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    107.         WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    108.        
    109.     }
    110.     else
    111.     {
    112.    
    113.         WheelRR.motorTorque = 0;
    114.         WheelRL.motorTorque = 0;
    115.        
    116.     }
    117.    
    118.     var speedFactor = rigidbody.velocity.magnitude / lowestSteerAtSpeed;
    119.     var currentSteerAngle = Mathf.Lerp(lowSpeedSteerAngle,highSpeedSteerAngle,speedFactor);
    120.     currentSteerAngle *=Input.GetAxis("Horizontal");
    121. //  WheelFL.steerAngle = 10 * Input.GetAxis("Horizontal");
    122. //  WheelFR.steerAngle = 10 * Input.GetAxis("Horizontal");
    123.     WheelFL.steerAngle = currentSteerAngle;
    124.     WheelFR.steerAngle = currentSteerAngle;
    125.  
    126. }
    127.  
    128. function WheelPosition() // For Changing position of wheel due to Suspension SPring
    129. {
    130.  
    131.     var hit : RaycastHit;
    132.     var WheelPos : Vector3;
    133.     if(Physics.Raycast(WheelFL.transform.position, -WheelFL.transform.up,hit,WheelFL.radius+WheelFL.suspensionDistance))
    134.     {
    135.    
    136.         WheelPos=hit.point+WheelFL.transform.up*WheelFL.radius;
    137.    
    138.     }
    139.     else
    140.     {
    141.    
    142.         WheelPos=WheelFL.transform.position-WheelFL.transform.up*WheelFL.suspensionDistance;
    143.    
    144.     }
    145.     WheelFLTrans.position=WheelPos;
    146.    
    147.     if(Physics.Raycast(WheelFR.transform.position, -WheelFR.transform.up,hit,WheelFR.radius+WheelFR.suspensionDistance))
    148.     {
    149.    
    150.         WheelPos=hit.point+WheelFR.transform.up*WheelFR.radius;
    151.    
    152.     }
    153.     else
    154.     {
    155.    
    156.         WheelPos=WheelFR.transform.position-WheelFR.transform.up*WheelFR.suspensionDistance;
    157.    
    158.     }
    159.     WheelFRTrans.position=WheelPos;
    160.    
    161.     if(Physics.Raycast(WheelRL.transform.position, -WheelRL.transform.up,hit,WheelRL.radius+WheelRL.suspensionDistance))
    162.     {
    163.    
    164.         WheelPos=hit.point+WheelRL.transform.up*WheelRL.radius;
    165.    
    166.     }
    167.     else
    168.     {
    169.    
    170.         WheelPos=WheelRL.transform.position-WheelRL.transform.up*WheelRL.suspensionDistance;
    171.    
    172.     }
    173.     WheelRLTrans.position=WheelPos;
    174.    
    175.     if(Physics.Raycast(WheelRR.transform.position, -WheelRR.transform.up,hit,WheelRR.radius+WheelRR.suspensionDistance))
    176.     {
    177.    
    178.         WheelPos=hit.point+WheelRR.transform.up*WheelRR.radius;
    179.    
    180.     }
    181.     else
    182.     {
    183.    
    184.         WheelPos=WheelRR.transform.position-WheelRR.transform.up*WheelRR.suspensionDistance;
    185.    
    186.     }
    187.     WheelRRTrans.position=WheelPos;
    188.  
    189. }
    190.  
    191. function Handbrake()
    192. {
    193.  
    194.     if(Input.GetButton("Jump"))
    195.     {
    196.    
    197.         braked=true;
    198.        
    199.     }
    200.     else
    201.     {
    202.    
    203.         braked=false;
    204.    
    205.     }
    206.     if(braked)
    207.     {
    208.    
    209.         WheelRR.brakeTorque = maxBreakTorque;
    210.         WheelRL.brakeTorque = maxBreakTorque;
    211.         WheelRR.motorTorque = 0;
    212.         WheelRL.motorTorque = 0;
    213.         if (rigidbody.velocity.magnitude > 1)
    214.         {
    215.        
    216.             SetSlip(slipForwardFriction,slipSidewayFriction);
    217.            
    218.         }
    219.         else
    220.         {
    221.        
    222.             SetSlip(1,1);
    223.        
    224.         }
    225.    
    226.     }
    227.     else
    228.     {
    229.    
    230.         WheelRR.brakeTorque = 0;
    231.         WheelRL.brakeTorque = 0;
    232.         SetSlip(myForwardFriction,mySidewayFriction);
    233.    
    234.     }
    235.  
    236. }
    237.  
    238. function SetSlip(currentForwardFriction : float,currentSidewayFriction : float)
    239. {
    240.  
    241.     WheelRR.forwardFriction.stiffness=currentForwardFriction;
    242.     WheelRL.forwardFriction.stiffness=currentForwardFriction;
    243. //  WheelFR.forwardFriction.stiffness=currentForwardFriction;
    244. //  WheelFL.forwardFriction.stiffness=currentForwardFriction;
    245.  
    246.     WheelRR.sidewaysFriction.stiffness=currentSidewayFriction;
    247.     WheelRL.sidewaysFriction.stiffness=currentSidewayFriction;
    248. //  WheelFR.sidewaysFriction.stiffness=currentSidewayFriction;
    249. //  WheelFL.sidewaysFriction.stiffness=currentSidewayFriction;
    250.  
    251.  
    252. }
    253.  
    254. function EngineSound ()
    255. {
    256.    
    257.     for(var i = 0; i < GearRatio.length; i++)
    258.     {
    259.    
    260.         if(GearRatio [i] > currentSpeed)
    261.             break;
    262.    
    263.     }
    264.     var gearMinVal : float = 0.0;
    265.     var gearMaxVal : float = 0.0;
    266.     if (!i)
    267.     {
    268.    
    269.         gearMinVal = 0;
    270.         gearMaxVal = GearRatio [i];
    271.    
    272.     }
    273.     else
    274.     {
    275.    
    276.         gearMinVal = GearRatio [i-1];
    277.         gearMaxVal = GearRatio [i];
    278.    
    279.     }
    280.     var enginePitch : float = ((currentSpeed - gearMinVal) / (gearMaxVal - gearMinVal))+1;
    281.     audio.pitch = enginePitch;
    282.    
    283. }
     
  8. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    From the script itself, I dont see how is (MoveFlag.isOver) altered. And dont understand why you are altering - this.rigidbody.isKinematic=true;.
    Also test if you are able to control your car with pressing vertical successively.

    Aur "accln nahi diya :" to kya doosri ya teesri bar acclnl dabane pe speed badalti hai kay?

    Thats all I can help with script part for now.
     
    Last edited: Apr 27, 2014
  9. Suddan

    Suddan

    Joined:
    Jan 12, 2013
    Posts:
    21
    Hey there again.

    I just had a closer look at it and i might have found something, i couldn't test it though since it would take a bit more time to set up an object for the script to test it.

    When there's no input, you set the braketorque to your deacceleration value in the control() function - (90 if you haven't changed it in the editor).
    So, at the moment your braketorque should actually influence the speed as you want it to.

    But as a next step, you call the brake() function which will assign zero again to the braketorque when the brake-key is not pressed. This, in case the brake-key is not pressed, you end up have having a braketorque of 0 again even though there is no acceleration input.
     
  10. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    @msl_manni
    :D :)
    the (MoveFlag.isOver) is actually used to check whether the countdown has ended or not "3...2...1...GO!"
    till the countdown is going on, I've programmed the vehicles to rigidbody.iskinematic = true, so that , user input doesn't affect the movement of car before coundown has ended....
    and that "accln nahi diya.." thing is just to check whether user has pressed the up/ down key, (hence, provided accln ?)or not? this text is accompanied with true/false value whether user has pressed vertical +ive or -ive button,...

    @Suddan
    thanks, I'll check the code again....and rectify it....and test it again...
     
  11. Droove92

    Droove92

    Joined:
    Jul 5, 2013
    Posts:
    23
    @Suddan Thanks a bunch mate!! :D
    Problem Solved!