I am making a 3D first person game, and I ran into a very annoying and unpredictable issue: The “Grounded” variable I use to check if the player is touching the floor does not work as it should, even after following a tutorial (Physics).
Using the logs, I found the reason for it. It seems the methods OnCollisionEnter and OnCollisionStay are not being called when they should. They should always be called whenever the player’s collider (a capsule collider) is touching another collider (in this case, a box collider), yet they aren’t being. Instead, only when the player moves around does the Grounded variable turns true, and when the player stands still it becomes false after some milliseconds for some reason.
This is the code I am using:
void OnCollisionEnter (Collision ObjectCollision)
{
print("enter");
EvaluateCollision (ObjectCollision);
}
void OnCollisionStay (Collision ObjectCollision)
{
print("stay");
EvaluateCollision (ObjectCollision);
}
void EvaluateCollision (Collision ObjectCollision)
{
for (int i = 0; i < ObjectCollision.contactCount; i++)
{
Vector3 ObjectNormal = ObjectCollision.GetContact(i).normal;
if (ObjectNormal.y >= SlopeMinLimit)
{
Grounded = true;
}
else
{
Grounded = false;
}
}
}
The Grounded variable is also automatically set to false at the end of FixedUpdate method (this should not be an issue however, it’s exactly the same approach used by the tutorial mentioned above and theoretically happens after the collision detections are calculated).