Created
February 26, 2022 23:24
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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