- Home /
Can't figure out how to detect ground (2D)
I've been stuck on this for a couple weeks. It's a project for one of my classes in college. I can't figure out how to detect the character colliding with the ground so it makes it that you only touch the ground once. I'm attaching all relevant code and will explain what it does. The first section is in void FixedUpdate, the code wasn't showing up right.
if ((jump == true) && (canjump == true))
{
if (((Input.GetKeyDown(KeyCode.UpArrow)) || (Input.GetKeyDown(KeyCode.Space))))
{
roomba.AddForce(Vector2.up * jumpPower);
}
}
}
public void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "PowerUp")
{
Destroy(col.gameObject);
jump = true;
}
}
public void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Floor")
{
canjump = true;
}
else
{
canjump = false;
}
}
So the first two functions are self explanatory. OnCollisionStay should check to see if you're colliding with the floor beneath you, and change canjump based on that, but it doesn't. I have no idea at this point how to fix this. Thank you in advance.
Answer by MelvMay · Apr 13, 2017 at 01:09 PM
public void OnCollisionStay(Collision collision)
This is a 3D physics callback.
I fixed it by making it OnCollisionStay2D(Collision2D collision) but that didn't solve it. That probably was important though, thank you for pointing that out
Your answer
Follow this Question
Related Questions
Character Fails To Jump Sometimes 1 Answer
Jumping on top of an enemy problem 1 Answer
Making player jump when left mouse clicked 1 Answer
Jumping mechanism 1 Answer
Jump higher when holding button 5 Answers