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

need help with simple problem

Discussion in 'Scripting' started by tallieke, Jul 1, 2015.

  1. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    unity, javascript

    hey guys, please help, this is a script for a racing game im making to control the car... my car is supposed to go forward, backwards, and turn both left and right well and it was working well but i must have messed something up because it wont turn left or right almost at all anymore. heres the code. any help is greatly appreciated :)


    Code (JavaScript):
    1. #pragma strict
    2. var wheelFL : WheelCollider;
    3. var wheelFR : WheelCollider;
    4. var wheelRL : WheelCollider;
    5. var wheelRR : WheelCollider;
    6. var wheelFLTrans : Transform;
    7. var wheelFRTrans : Transform;
    8. var wheelRLTrans : Transform;
    9. var wheelRRTrans : Transform;
    10. var maxTorque : float = 50;
    11. function Start () {
    12. rigidbody.centerOfMass.y = -0.10;
    13. }
    14.  
    15. function FixedUpdate () {
    16. wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
    17. wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    18. wheelFL.motorTorque = maxTorque * Input.GetAxis("Horizontal");
    19. wheelFR.motorTorque = maxTorque * Input.GetAxis("Horizontal");
    20. }
    21. function Update () {
    22. wheelFLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
    23. wheelFRTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
    24. wheelRLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
    25. wheelRRTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
    26. }
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I would guess, you need to change line 22 - 25, as they all are asking for the wheelFL.rpm. Maybe you need to change it in line 23 to wheelFR and so on? It's not a good point to start solving a problem with you "messed up somehow". I advice you using TortoiseSVN or any other subversion control system, so you can go back to previous versions of your script / scenes whatever and check, what you have changed...
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you tapping left/right? fixedUpdate doesn't run every frame but at a fixed interval, if your input comes on a frame where fixedUpdate didn't run it wont get picked up. Input really should be used in the Update function...
     
  4. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    its all good but i have another problem, this line is supposed to stabilize the car so when im moving fast and i turn sharp it wont go all wonky on me (does bycicles, flips and even bicycles on the wrong side... looks very stupid and unplayable). this line did a little bit but didnt stabilize it well enouph. any suggestions... please make it simple :)

    rigidbody.centerOfMass.y = -0.09;