Function called when a GameObject doesn't collide with ANYTHING
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.
In case you're wondering, I ask this as just using OnCollisionExit has led to an issue in a project I am creating where if you bump into a wall the second you leave the wall the OnCollisionExit function is called, which I only want called when you aren't touching anything
Answer by Hellium · Jul 13, 2019 at 07:42 AM
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" );
}
}
}
Would have never gotten that on my own. Thank you so much!!
Your answer
Follow this Question
Related Questions
Simple 3D collision issue 0 Answers
Collision between objects not being detected 0 Answers
Who collided first 0 Answers
How to make a bowl? 0 Answers