3
\$\begingroup\$

I have researched some examples of anti roll bars for Unity but they are all for unity 4 and bellow so they do not work in unity 5. Can someone please assist me in making an anti roll bar script in C# for Unity 5?

Here is the best example I could find (I don't know how to convert it for unity 5):

using System.Collections;

public class AntiRollBar : MonoBehaviour {

  var WheelL : WheelCollider;
  var WheelR : WheelCollider;
  var AntiRoll = 5000.0;

  // Use this for initialization
  void Start () {

  }

  // Update is called once per frame
  void fixedUpdate () {

    var hit : WheelHit;
    var travelL = 1.0;
    var travelR = 1.0;

    var groundedL = WheelL.GetGroundHit(hit);
    if (groundedL)
      travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;

    var groundedR = WheelR.GetGroundHit(hit);
    if (groundedR)
      travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;

    var antiRollForce = (travelL - travelR) * AntiRoll;

    if (groundedL)
      rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
                                   WheelL.transform.position); 
    if (groundedR)
      rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
                                   WheelR.transform.position); 
  }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ what errors are you getting ? \$\endgroup\$
    – Uri Popov
    May 16, 2016 at 8:26
  • \$\begingroup\$ @UriPopov If possible please review my new code to check if it is valid because i have no way of knowing \$\endgroup\$
    – J.Clarke
    May 17, 2016 at 1:57
  • \$\begingroup\$ For one, you seem to be trying to use the FixedUpdate(), but you call fixedUpdate(), instead. \$\endgroup\$
    – Gnemlock
    Nov 27, 2016 at 20:32

3 Answers 3

6
\$\begingroup\$

You found your example here, I guess. I translated it in C# for Unity 5, and it works fine.

using UnityEngine;
using System.Collections;

public class AntiRollBar : MonoBehaviour 
{   
    public WheelCollider WheelL;
    public WheelCollider WheelR;
    private Rigidbody carRigidBody;

    public float AntiRoll = 5000.0f;

    void Start ()
    {
        carRigidBody = GetComponent <Rigidbody>();
    }

    void FixedUpdate ()
    {
        WheelHit hit = new WheelHit();
        float travelL = 1.0f;
        float travelR = 1.0f;

        bool groundedL = WheelL.GetGroundHit(out hit);

        if (groundedL)
        {
            travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y 
                    - WheelL.radius) / WheelL.suspensionDistance;
        }

        bool groundedR = WheelR.GetGroundHit(out hit);

        if (groundedR)
        {
            travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y 
                    - WheelR.radius) / WheelR.suspensionDistance;
        }

        var antiRollForce = (travelL - travelR) * AntiRoll;

        if (groundedL)
            carRigidBody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
                WheelL.transform.position); 
        if (groundedR)
            carRigidBody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
                WheelR.transform.position); 
    }

}
\$\endgroup\$
0
\$\begingroup\$
using UnityEngine;
using System.Collections;

public class SwayBar : MonoBehaviour
{
public WheelCollider WheelL;
public WheelCollider WheelR;
public GameObject carRigidBody; //Car with rigidbody attached
private Rigidbody rb;

public float AntiRoll = 5000.0f;

void Start()
{
    rb = carRigidBody.GetComponent<Rigidbody>();
}

void FixedUpdate()
{
    WheelHit hit = new WheelHit();
    float travelL = 1.0f;
    float travelR = 1.0f;

    bool groundedL = WheelL.GetGroundHit(out hit);

    if (groundedL)
    {
        travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y
                - WheelL.radius) / WheelL.suspensionDistance;
    }

    bool groundedR = WheelR.GetGroundHit(out hit);

    if (groundedR)
    {
        travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y
                - WheelR.radius) / WheelR.suspensionDistance;
    }

    var antiRollForce = (travelL - travelR) * AntiRoll;

    if (groundedL)
        rb.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
            WheelL.transform.position);
    if (groundedR)
        rb.AddForceAtPosition(WheelR.transform.up * antiRollForce,
            WheelR.transform.position);
    }
}

I used mine on the on a empty gameobject of its own called swaybar attached inside the car so a few changes from the above one to suit. Add it twice and you can include rear right and left in one script and front right and left in the other.

\$\endgroup\$
-1
\$\begingroup\$

I attempted to code my own script and it apears to make the car a little more physically stable

using UnityEngine; using System.Collections;

public class Anti_Roll : MonoBehaviour {

public WheelCollider wheelL;
public WheelCollider wheelR;
public Rigidbody rb;
public float force;
public float position;

// Use this for initialization
void Start () {
    rb = GetComponent<Rigidbody>();

}

// Update is called once per frame
void Update () {
    RaycastHit hit;
    RaycastHit hit2;
    //Left wheel
    Ray cast = new Ray (wheelL.transform.position, -Vector3.up);
    Physics.Raycast (cast, out hit);
    float wheelL_force = hit.distance;
    //Right Wheel
    Ray cast2 = new Ray (wheelR.transform.position, -Vector3.up);
    Physics.Raycast (cast2, out hit2);
    float wheelR_force = hit2.distance;

    float applyL = wheelL_force - wheelR_force;
    float applyR = wheelR_force - wheelL_force;
    Debug.Log ("Difference:" + (wheelL_force - wheelR_force));
    rb.AddForceAtPosition (Vector3.down*applyL*force, (rb.position - wheelL.transform.position)*position);
    rb.AddForceAtPosition (Vector3.down*applyR*force, (rb.position - wheelR.transform.position)*position);


}

}

\$\endgroup\$
4
  • \$\begingroup\$ I think you are missing a chunk of code friend. \$\endgroup\$
    – Uri Popov
    May 17, 2016 at 7:12
  • \$\begingroup\$ @ Uri Popov I don't understand all I am trying to do is the the compression from one suspension and put a little of that on the other side how do I do it? \$\endgroup\$
    – J.Clarke
    May 17, 2016 at 10:35
  • 1
    \$\begingroup\$ What I wanted to say is that I think you have not pasted the whole script in your answer. For me it ends on the line 'car_rb.AddForceAtPosition (Vector3.down * force, transform.position);' but I think there is more code after that.Please edit your answer. \$\endgroup\$
    – Uri Popov
    May 17, 2016 at 12:36
  • \$\begingroup\$ Your code appears cut off. \$\endgroup\$
    – Gnemlock
    Nov 27, 2016 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .