Created
April 23, 2020 13:06
-
-
Save Arakade/26fd73243920ca1c2c8fe6888e2bf70a to your computer and use it in GitHub Desktop.
Log when collisions and triggers are entered and exited.
This file contains hidden or 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 System.Linq; | |
using System.Runtime.CompilerServices; | |
using UnityEngine.Assertions; | |
using UnityEngine; | |
namespace UGS.unityutils { | |
/// <summary> | |
/// Log when collisions and triggers are entered and exited. | |
/// </summary> | |
public sealed class CollisionHelper : MonoBehaviour { | |
#if UNITY_EDITOR | |
#region serialized | |
#endregion serialized | |
#region Unity callbacks | |
public void OnValidate() { | |
Assert.IsNotNull(GetComponent<Rigidbody>(), $"{name} has no rb"); | |
} | |
public void OnCollisionEnter(Collision info) { | |
log($"{info.gameObject.name}: " + string.Join("\n - ", info.contacts.Select(c => $"{c.otherCollider} {c.point}"))); | |
} | |
public void OnCollisionExit(Collision info) { | |
log($"{info.gameObject.name}: " + string.Join("\n - ", info.contacts.Select(c => $"{c.otherCollider} {c.point}"))); | |
} | |
public void OnTriggerEnter(Collider info) { | |
log(info); | |
} | |
public void OnTriggerExit(Collider info) { | |
log(info); | |
} | |
#endregion Unity callbacks | |
#region public | |
#endregion public | |
#region private | |
private void log(object arg, [CallerMemberName] string callerName = "UNKNOWN") { | |
Debug.Log($"{Time.frameCount}: {name}.{callerName}({arg})", this); | |
} | |
#endregion private | |
#endif // UNITY_EDITOR | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment