- Home /
Delay for collision detection
I'm using OnCollisionEnter() and OnCollisionExit() to add collisions to a list. Every tick, a method determines if the player is grounded by analyzing the items in the list. When I jump, I am still considered grounded even when I have a great vertical velocity component. Playing with the collision detection mode doesn't fix anything either. Manually setting isGrounded to false when the jump method is called doesn't work either as the method that determines the grounding state overrides it to true (as there are still collisions even though I have a great vertical velocity component). This allows the player is jump two times. Below is heavily truncated code.
public class Movement : MonoBehaviour
{
public List<Collision> collisions = new List<Collision>();
public bool isGrounded = false;
private void FixedUpdate()
{
// update grounding state
CheckGroundingState();
// update acceleration
if (isGrounded) {
ApplyGroundAcceleration();
}
else {
ApplyAirAcceleration();
}
// update jump
if (Input.GetButton("Jump") && isGrounded) {
Jump();
}
}
private void CheckGroundingState() { ... }
private void OnCollisionEnter(Collision collision) { ... }
private void OnCollisionExit(Collision collision) { ... }
}
In the collision methods, the collisions are added or removed from the list. In the CheckGroundingState() method, each collision in the list is analyzed. If it appears to be a ground, it sets isGrounded to true. And no, calling CheckGroundingState() in the collision methods and not in the FixedUpdate() method will not work. This seems like a dead end. Any ideas?
Answer by finlay_morrison · May 03, 2018 at 12:14 PM
Start a coroutine in the oncollisionenter that has a wait time of x amount of seconds
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnCollisionExit() too slow 1 Answer
Make object pass through one object and bounce from another 2 Answers