Skip to content

Instantly share code, notes, and snippets.

@Arakade
Created April 23, 2020 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arakade/26fd73243920ca1c2c8fe6888e2bf70a to your computer and use it in GitHub Desktop.
Save Arakade/26fd73243920ca1c2c8fe6888e2bf70a to your computer and use it in GitHub Desktop.
Log when collisions and triggers are entered and exited.
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