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

Car keeps bouncing randomly with 'Stabilizer Bars' code

Discussion in 'Scripting' started by Bernie-Buddy, Dec 10, 2014.

  1. Bernie-Buddy

    Bernie-Buddy

    Joined:
    Apr 8, 2014
    Posts:
    6
    I have no idea what's going on, my car just bounces up dramatically for seemingly no reason when I have this code on:

    Code (csharp):
    1. public class StabilizerBars : MonoBehaviour {
    2.  
    3.     WheelCollider FL, FR, BL, BR;
    4.     float AntiWheelForce = 200;
    5.     public float TravelL = 1.0f;
    6.     public float TravelR = 1.0f;
    7.     public float AntiRollForce;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         FL = GameObject.Find ("Wheel_FL").GetComponent<WheelCollider>();
    12.         FR = GameObject.Find ("Wheel_FR").GetComponent<WheelCollider>();
    13.  
    14.         BL = GameObject.Find ("Wheel_BL").GetComponent<WheelCollider>();
    15.         BR = GameObject.Find ("Wheel_BR").GetComponent<WheelCollider>();
    16.     }
    17.     // Update is called once per frame
    18.     void Update () {
    19.         WheelHit hit;
    20.         bool GroundL = FL.GetGroundHit(out hit);
    21.         bool GroundR = FR.GetGroundHit(out hit);
    22.  
    23.         if (GroundL) {
    24.                         TravelL = (-FL.transform.InverseTransformPoint (hit.point).y - FL.radius) / FL.suspensionDistance;
    25.                 }
    26.         if (GroundR) {
    27.             TravelR = (-FR.transform.InverseTransformPoint(hit.point).y - FR.radius) / FR.suspensionDistance;
    28.         }
    29.  
    30.         AntiRollForce = (TravelL - TravelR) * AntiWheelForce;
    31.  
    32.  
    33.         print (AntiRollForce);
    34.  
    35.         if (GroundL) {
    36.             gameObject.rigidbody.AddForceAtPosition(
    37.                 FL.transform.up * -AntiRollForce,
    38.                 FL.transform.position);
    39.                 }
    40.         if (GroundR) {
    41.             gameObject.rigidbody.AddForceAtPosition(
    42.                 FR.transform.up * AntiRollForce,
    43.                 FR.transform.position);
    44.         }
    45.  
    46.     }
    47. }
    It also doesn't stablize the car at all.
    For now it's only enabled for the front wheels. I made it do the back wheels earlier and the same thing happened.
    Does anyone know why this happens?
     
    Last edited: Dec 10, 2014
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    Does it work any better if you do it in the FixedUpdate method instead of Update
     
  3. Bernie-Buddy

    Bernie-Buddy

    Joined:
    Apr 8, 2014
    Posts:
    6
    No, it seems to do the same thing.

    Edit: Actually, yeah it seems to be doing something now after I apply it to the back wheels as well.
    It's still bouncing, though. I can't really tell why it's doing it.
     
    Last edited: Dec 10, 2014
  4. Bernie-Buddy

    Bernie-Buddy

    Joined:
    Apr 8, 2014
    Posts:
    6
    I am still having this problem, my car acts exactly as it did without the stabilizers (it easily rolls over), except now it has even less stabilization with the random popping.

    I set AntiRollForce to print so I could see what was happening, it stays at values lower than 0.001 and then randomly spikes up to the hundreds. I have no idea what to do.