How to detect if not colliding
The game needs to know if the player is colliding or not but it only detects if it is colliding but not if its not so how do that someone told me to use OnCollisionExit but it wont work :(
void OnCollisionEnter(Collision col)
{
Debug.Log("On the ground");
}
void OnCollisionExit(Collision col)
{
Debug.Log("On the ground");
}
}
$$anonymous$$ight be hard to notice even if it was working when you have the same logging in both methods.
Are you sure it's not working? There's no reason it shouldn't work if Enter does.
Sorry i dint notice i did that :3 it suposed to bev ("Off the ground")
I changed it but now it will only say "Off the ground" :(
Answer by KenjiKyo · Jul 26, 2016 at 12:52 AM
OnCollisionEnter and OnCollisionExit require that the GameObject you attached this script to have a Collider. Did you make sure to add one.
Also, these functions are "oneshots", in the sense that they will be called once when the event occurs but afterwards the collision state is lost if you didn't save it anywhere.
Personally, I like to use a counter to keep track of all Enter/Exit events to make sure that my object is not colliding with anything in case it would hit more than one other objet simultaneously or if multiple Enter events occur one after the other for multiple objects overlap without getting the Exit events of the first ones hit yet.
You could try this:
pulic class SomeScript
{
private int collisionCount = 0;
public bool IsNotColliding
{
get { return collisionCount == 0; }
}
void OnCollisionEnter(Collision col)
{
collisionCount++;
}
void OnCollisionExit(Collision col)
{
collisionCount--;
}
}
Your answer
Follow this Question
Related Questions
New Hierarchy Son Script 0 Answers
[emergency]Know collision between child object of two object 1 Answer
[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards 0 Answers
How to define when object is inside another object (2D)? 0 Answers
My collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers