I want to make a function called when the GameObject is NOT colliding with ANYTHING, as opposed to stopped colliding with one other GameObject.
I’d really appreciate a response of some sort, as my questions never seem to get answered.
Basically, I wanna do this:
void OnCollisionStay()
{
//blah blah blah
}
else
{
//blah blah blah
}
Edit: I’d be fine and even prefer if it was a function called every frame the GameObject isn’t colliding with anything, kinda like a function called every frame that OnCollisionStay isn’t.
using System.Collections.Generic;
using UnityEngine;
public class CollisionDetection : MonoBehaviour
{
private HashSet<int> collidingGameObjects = new HashSet<int>();
private void OnCollisionEnter( Collision collision )
{
collidingGameObjects.Add( collision.gameObject.GetInstanceID() );
}
private void OnCollisionExit( Collision collision )
{
collidingGameObjects.Remove( collision.gameObject.GetInstanceID() );
}
private void OnDisable()
{
collidingGameObjects.Clear();
}
private void Update()
{
if( collidingGameObjects.Count == 0 )
{
Debug.Log( "I am not colliding with anything" );
}
}
}