How to use Collision.GetContacts

I’m trying to detect collisions from my projectile against multiple colliders. It’s a cone shaped sort of projectile and can hit about 3 - 4 cubes evenly spaced apart side by side. However only one of the colliders hit receives damage and displays my hit effect. I read up what little I could find in the scripting api about Collision.GetContacts but I have no idea how to properly use it.

Here’s what I got right now:


public float speed;
public float returnSpeed;
public Transform projHitClone;

    private Attack_Master attackMaster;
    private ContactPoint[] contacts;

    void OnEnable()
    {
        attackMaster = GetComponent<Attack_Master>();
    }

    void Update()
    {
        if(speed != 0)
        {
            transform.position += transform.forward * (speed * Time.deltaTime);
        }
    }

    void OnSpawned()
    {
        speed = returnSpeed;
    }

    void OnCollisionEnter(Collision col)
    {
        if (col.transform.CompareTag("Enemy"))
        {
            attackMaster.CallEventHitEnemy(col.transform);
        }
        foreach(ContactPoint contact in contacts)
        {
            Debug.Log("Collided");
            col.GetContacts(contacts);
            Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
            Vector3 pos = contact.point;
            speed = 0;
            Pooly.Spawn(projHitClone, pos, rot);
        }
        contacts = new ContactPoint[15];
    }

This doesn’t work at all I just get a null object reference whenever my projectile collides. How do I basically say that for each collider hit, apply damage and spawn my hit effect along the contacts normal?

Thanks to xxmariofer!


void OnCollisionEnter(Collision col)
        {
            if (col.transform.CompareTag("Enemy"))
            {
                attackMaster.CallEventHitEnemy(col.transform);
                enemies.Add(col.transform);
            }

            Debug.Log("Collided");
            ContactPoint contact = col.GetContact(0);
            Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
            Vector3 pos = contact.point;
            speed = 0;
            Pooly.Spawn(projHitClone, pos, rot);
        }

Here’s an incomplete snippet of code I just wrote for the same issue. Basically init an array size of the contact count then for loop it.

ContactPoint[] contact = new ContactPoint[col.contactCount];
int points = col.GetContacts(contact);
for (int i = 0; 0 < points; i--)
        {
            ContactPoint cp = contact*;*

if (GetComponent().gameObject != gameObject)
continue;
}