Bug in game

I created replica of Doodle Jump and what happens is that when Im jumping character lands on platform and doesn’t add force on Y axis, I think bug is about colliders but Im not sure. Anyone knows how to fix this ? Here is video how it looks like and screnshoots of platform and character settings.gq8je


Show us the code you’re using to perform a jump. Is the intent that jumping is automatic any time the character touches the ground? How and when are you triggering the jump? OnCollisionEnter with a collider? In that case, maybe you’re hitting the side of the collider, and you aren’t grounded.

It seems the safest way would be to do the jumping in FixedUpdate, jumping any time the character isn’t grounded. But, let’s see the code, otherwise I’m just guessing at your situation.

This is code, it is supposed that character jump OnCollisionEnter, but it’s possible that on landing it hits side of collider and then gets stuck. Second screenshot is how collider looks.

3485871--277454--Screenshot_1.jpg
3485871--277455--Screenshot_2.jpg

Probably the simplest tweak to get this working would be to add an OnCollisionStay2D method that does the same thing as your OnCollisionEnter2D method. Put the actual code into a method have have OnCollisionEnter2D and OnCollisionStay2D both call that method. The “Stay” version fires continuously while the collider is still in contact, so it should handle the case where you glide onto the platform after colliding with the edge of it.

Doing that you might notice the jump firing more than once, because OnCollisionStay might fire again before the collider is properly in the air. So I’d recommend you keep a local float variable called something like _lastJumpedAt, and set it to Time.timeSinceLevelLoad each time you jump. But in your jump logic, only trigger a jump if a small amount of time (say 0.1 seconds) has passed since the last jump. That should take care of the double jumping issue.

An entirely different approach, which is what I use for jumping, is to perform a Raycast downward in FixedUpdate. If your Raycast hits something, you can consider yourself “on the ground”, in which case you can trigger a jump. If the raycast doesn’t hit anything, you’re in the air, and you don’rt apply any jump forces. Just another way you could handle this.

I added OnCollisonStay2D without variable _lastJumpedAt, and it doesn’t jump two times but sometimes character still gets stuck on platform but if i keep moving left and right it jump at some moment. I’m new and not really familiar with Raycast so if you could help me with that. :slight_smile:

The basic idea with Raycast is that the physics system will draw a line in a direction, and see if it intersected any colliders. If it intersected a collider, you know there’s something in that direction, such as the ground. To check for the ground, you’d start from a point just above the bottom of your character’s feet, going down a short distance. For example:

3486347--277534--upload_2018-5-5_13-9-57.png

Assume that the orange capsule is your character’s collider, and the green line is the ground. The blue line represents a Raycast starting just above the character’s position, and going down a short distance, like 0.01 units. If that raycast hits something, you can consider yourself grounded, and you can trigger a jump.

So that’s the basic idea. The risk with Raycasting is that if you’re on the very edge of a platform, the raycast might not hit anything, in cases where the center of your collider is too far off the edge. If you run into issues with that, you can use a CircleCast instead of a raycast (Unity - Scripting API: Physics2D.CircleCast). This is more likely to hit things. But start with a raycast. The example code on (Unity - Scripting API: Physics2D.Raycast) looks to be almost exactly what you’re trying to do: auto-jump when grounded.

It looks like I found a solution thanks to you. Looks like problem was that character collider was too large and when it landed on edge of platform that bug would happen. Thank you very much :smile: