- Home /
How to make player detect when on ground and when lands on ground
I am making a 2D platformer and I'm having trouble making the player be able to jump. How can I make my player detect when he's on the ground so he can jump? How can he detect when he lands on ground after jumping for a landing animation?
I need to make this Platform Effector proof
Answer by NNNscarfo · Sep 29, 2017 at 07:58 PM
There are generally two ways to do this. You could attach Colliders on the feet of your character and use them as triggers, checking when they intersect with the floor. Or, you could use RayCast2D to find out how far away the floor is from the feet of your character (by casting rays downward from their feet).
How would I make not accidentally make the player think he hits the ground when he's just colliding with one way platform effectors?
I don't have much experience with Platform Effectors but my understanding is that you should be fine. I think those considerations are built into the Platform Effector component.
In some cases you can give different objects different tags or same name and OnCollision check the collided objects name or tag?
For example ground tag for ground objects then check if(collision.gameObject.tag == somethingString)
Answer by avi_spc · Sep 29, 2017 at 08:12 PM
You can use a bool variable in OnCollisionStay method and when it is in collision with ground make that variable to be true else make it false.
Answer by Jadlar · May 12, 2020 at 10:49 PM
@Raja-Unity This is the best way I found to check for landing. All you have to do is make sure you call CheckLand() in Update before CheckAirTIme()
Blockquote
private void CheckAirTime()
{
if (charController.isGrounded)
{
airTime = 0f;
}
else
{
airTime += Time.deltaTime;
}
}
private void CheckLand()
{
if (airTime > 0)
{
if (charController.isGrounded)
{
//Debug.Log("Landed");
}
}
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Animator is not playing a Playable 2 Answers
Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer
How do I access RectTransform attributes via animated script? 1 Answer
Playing list of animations 0 Answers