Skip to content

Instantly share code, notes, and snippets.

@nailuj05
Created February 26, 2022 23:24
Show Gist options
  • Save nailuj05/a31825b8675f59915dfd8217427509e1 to your computer and use it in GitHub Desktop.
Save nailuj05/a31825b8675f59915dfd8217427509e1 to your computer and use it in GitHub Desktop.
A component that allows you to subscribe to the Collision Events on a object (even if the main script does not lie on the object with the collider)
using UnityEngine;
//A component that allows you to subscribe to the Collision Events on a object (even if the main script does not lie on the object with the collider)
public class CollisionDetector : MonoBehaviour
{
public delegate void Coll(Collider collider);
public event Coll CollisionEnter;
public event Coll CollisionStay;
public event Coll CollisionExit;
void Start()
{
if(!TryGetComponent(out Collider coll))
{
Debug.LogError($"No collider found on {gameObject.name}");
}
}
private void OnCollisionEnter(Collision collision)
{
CollisionEnter(collision.collider);
}
private void OnCollisionStay(Collision collision)
{
CollisionStay(collision.collider);
}
private void OnCollisionExit(Collision collision)
{
CollisionExit(collision.collider);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment