In my game, the layers set to be ground are the default ground layer and the one way platform layer, and the jumping animation is set to be true if it’s not colliding with the ground layers and to be false if it’s colliding with them. The problem is, even if I’m in middle air , is detected that I’m colliding with the one way platform layer and the animation goes back to idle. I’ve already set the isGrounded bool to be true only if my velocity in the y axis is equals to 0 , but it clashes with the slopes. Does anyone know how can I fix this? Maybe changing the condition of the jumping animation of being false?
*Hi @Hemessfell *
“Does anyone know how can I fix this?”
TBH - fix what exactly? you haven’t shared any code so it is mostly guesswork even if you explained your case.
Making proper grounded state is IMO tricky, so it would be easier to understand your exact situation if you shared the code.
I wouldn’t just use velocity, but also do some raycast / cast, overlap test or collision check to decide if I’m grounded.
Slopes need more than one raycast if your character bounding box moves along its corners.
Animation shouldn’t usually matter at all when you are dealing with collisions.
Here’s what I’m doing for checking the ground
isGrounded = Physics2D.BoxCast(groundPosition.position, new Vector2(boxSize.x, boxSize.y), 0, Vector2.down, 1f, whatIsGround);
What is the distance parameter in that method call? Is it that second last 1f? I’m asking as I don’t have Unity open / that like of code in IDE.
Besides, that line alone won’t be quite enough to understand what you have done, so one can only guess what else you have done.
The distance parameter is as you said. The groundPosition is located right on the feet of the character. Here’s how my animation is set:
anim.SetBool("isGrounded", isGrounded);
So if I’m not grounded , the jumping animation occurs. Going back to my problem, as I said, the layers set to be ground are the default ground layer and the one way platform layer, so my problem is that even if I’m in middle air , when I pass through the one way platform the ground is checked and my character quickly goes to the idle animation before continuing the jumping animation
Well you could check velocity in addition to your current checks. While vertical velocity is positive, don’t check for floor collision (= grounded) unless you hit your head in the ceiling.
It will clash with slopes and other types of irregular terrain , even if it has a slightly uneven in the collider, the checks won’t check for collisions , so if I’m using a edge collider, for instance, that’s not gonna work. On plain terrains it works perfectly fine tho.
“It will clash with slopes and other types of irregular terrain”
Like I said, I would do it in addition to typical “ground/in air” check, not alone - can you be walking in ramp while you jump? No. Walking uphill or downhill is not the same as jumping.
You might wanna look into Platform Effector2D specifically the One-Way functionality.
You can also utilize Physics2D.IgnoreLayerCollision() to further insure you do not collide with the collider. Add another collider to the feet of your player so when it passes through the collider you can reset IgnoreLayerCollider so that your player can stand on the collider again.
Edit:
I just read that you are already using one way collider. My fault.
Just a thought. Why not stop your box cast when you jump? Maybe cut it back on when max height has been achieved or something along them lines.
That wouldn’t work because I need to fall through the platform as well
It would work if you use OnCollisionStay() to determine the platform the player is on, then when you hit the “fall” button, you disable the collider for X amount of time or OnCollisionExit() to enable it again.