- Home /
Double collision on "ground" tagged object results in player thinking they're in air?
I've had this issue plenty of times, unfortunately. If I set up a platformer with an object that detects ground, my code works perfectly unless two items tagged "ground" are next to each other, ie a floor and some stairs. This results in the animation, which reacts to this collision, keeping the player thinking it is in the air after switching from ground to ground.
Here's the section of the player code that controls the jumping and ground.
public void Jump() { if (jumps > 0) { GetComponent().AddRelativeForce(0, 300, 0); Anim.SetBool("grounded", false); jumps --; } }
private void OnCollisionEnter(Collision collision) { Anim.SetBool("grounded", true); jumps = maxJumps; }
And here is the culprit, the code on the trigger for ground check.
public class CheckGround : MonoBehaviour
{
public Animator Anim;
public GameObject AudioHolder;
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Ground"))
Anim.SetBool("grounded", false);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Ground"))
{
Anim.SetBool("grounded", true);
AudioHolder.GetComponent<PlayerSoundFX>().LandNoise();
}
}
}
Answer by AaronBacon · Dec 06, 2021 at 05:25 PM
The simplest way to fix this is to use OnTriggerStay instead of OnTriggerEnter. That will be called continuously while the object remains on the ground, however, I can see you're also playing a sound on landing on the ground, so you may need to use all 3, Enter for the sound effect, and Exit and Stay for the grounded Check:
⠀
public class CheckGround : MonoBehaviour
{
public Animator Anim;
public GameObject AudioHolder;
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Ground"))
Anim.SetBool("grounded", false);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Ground"))
{
AudioHolder.GetComponent<PlayerSoundFX>().LandNoise();
}
}
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Ground"))
{
Anim.SetBool("grounded", true);
}
}
}
⠀
Your answer
Follow this Question
Related Questions
Add a bool to animator & activate by key press 1 Answer
Triggering a Player Hit Animation 2 Answers
Animtor Stuck 1 Answer
How to check how many times an animation has played? 1 Answer
Animator parameter stuck as True 4 Answers