#2D Raycast Collision Trouble

1 messages · Page 1 of 1 (latest)

shell spear
#

Hey all,

I'm trying to iron out the basics of my raycast collision before I add stuff like coyote time, double jumps, etc.. It seems to be almost working perfectly, but there's one tiny issue (and at least one potential issue) that I'm seeing:

When the player lands after a jump or a fall, then occasionally go from grounded (for probably one frame), to not grounded (another frame), to grounded. It's extremely minor, but I want to stamp it out. It seems to happen more if I jump from a high platorm to a low one or a low one to a high one. I tried for a few minutes to get it to happen on flat ground, but no luck.

I'm really stumped here, so any pointers would be appreciated.

Video: https://youtu.be/wdZKlKw3M2I
Code: https://paste.ofcode.org/nbYFBSRFszxzQuiFp2MDsU

Skip to 0:41

Occasionally, when the player lands, they briefly alternate from grounded to not grounded, back to grounded again. The colour of the sprite changes with the "grounded" value.

▶ Play video
wise parcel
# shell spear Hey all, I'm trying to iron out the basics of my raycast collision before I add...

Likely just a precision issue and the best way to really combat a lot of this stuff is just throw more logical checks at it, or add some extra leeway instead of being exact. Meaning even if you expect your character to be leveled at the collider, you may want to consider your character grounded a few pixels above the ground as you jump off of it. Of course this may present more problems like being able to jump again even though you may be off the ground, which in that case you'll have to add additional logic to prevent that.

shell spear
wise parcel
#

Also, statemachines would really help to debug these issues as you'll be able to trace it better as you'll be able to trace it down into a state.

#

Instead of independent methods sequenced across the update

shell spear
#

My method actually has a "close enough" value built-in. The key part to make it work is that if the distance that the player wants to move is greater than the "skin", only allow movement up to that point (dst - skin). What I failed to do was include a "grounded = true" line after that. Stupid mistake.

shell spear
wise parcel
#

It's just a way to group logic a bit more instead of having it expanded upon the Update

#

Just a suggestion, not a requirement but I find it helpful when stepping through code that's for sure

#

Unity's animator controller forces a statemachine on you anyway, so mirroring your backend with it may give you some ideas

shell spear
#

I see. Yes, I should have it cleaned up and grouped into functions before I get carried away.

shell spear
wise parcel
#

yeah, I actually dislike it for sprite animation and prefer legacy, but it has uses if you chop your sprites into different limbs like say terraria