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

Sphere collider car, i need help for suspension part.

Discussion in 'Physics' started by radialxawn, May 19, 2019.

  1. radialxawn

    radialxawn

    Joined:
    May 13, 2019
    Posts:
    29
    Hi every one.
    I decided to make an arcade car with sphere collider instead of raycast. This video show the current result.
    I need help for the suspension part.
     
    Last edited: May 19, 2019
  2. radialxawn

    radialxawn

    Joined:
    May 13, 2019
    Posts:
    29
    Well! I managed it myself : ) haha
     
  3. NikoLaws

    NikoLaws

    Joined:
    Aug 26, 2019
    Posts:
    1
    Hey, working on something similar, could you share how you accomplished it?
     
  4. Elharter

    Elharter

    Joined:
    Sep 20, 2015
    Posts:
    58
    Yes please, would be great
     
  5. Modafuka

    Modafuka

    Joined:
    Nov 21, 2019
    Posts:
    45
    The basic idea: You will need a WheelColliderParent.cs and a WheelCollider.cs.
    WheelColliderParent.cs (attached to rigidbody) will send collision data (contact points) gathered from OnCollision(Enter, Stay, Exit) to WheelCollider.cs (attached to wheel with SphereCollider in it). Like this:
    Code (CSharp):
    1. public class WheelColliderParent : MonoBehaviour {
    2.    private WheelCollider[] wcArray;
    3.    private int numCtp = 0;
    4.    private ContactPoint[] ctpArray = new ContactPoint[512];
    5.    private List<ContactPoint> ctpList = new List<ContactPoint>(512);
    6.    private void OnCollisionEnter(Collision cls) {
    7.       cls.GetContacts(ctpList);
    8.       for (int i = 0; i < cls.contactCount; i++) { ctpArray[numCtp++] = ctpList[i]; }
    9.    }
    10.    private void OnCollisionStay(Collision cls) {
    11.       cls.GetContacts(ctpList);
    12.       for (int i = 0; i < cls.contactCount; i++) { ctpArray[numCtp++] = ctpList[i]; }
    13.    }
    14.  
    15.    private void OnCollisionExit(Collision cls) {
    16.       cls.GetContacts(ctpList);
    17.       for (int i = 0; i < cls.contactCount; i++) { ctpArray[numCtp++] = ctpList[i]; }
    18.    }
    19.  
    20.    private void FixedUpdate() {
    21.       for (int i = 0; i < numCtp; i++) {
    22.          for (int j = 0; j < wcArray.Length; j++) {
    23.             if (wcArray[j].sphereCollider.GetInstanceID() == ctpArray[i].thisCollider.GetInstanceID() { wcArray[j].NewContactPoint = ctpArray[i]; } // manage this method your self.
    24.          }
    25.       }
    26.       for (int i = 0; i < wcArray.Length; i++) {
    27.          wcArray[i].ManualFixedUpdate();
    28.       }
    29.       numCtp = 0;
    30.    }
    31. }