I’m making a 2D game, where the player has some animations: idle, run, jump, crouch.
For some reason when the player is killed, I load the scene again (which I don’t know if it’s bad practice or smth bad) and my jump animation stops working. When I jump it plays the animation for half a second and then mid air it instantly goes back to running (the running animation is played in jumping). I don’t need assistance or help for fixing the animations because I previously had this problem always and fixed the animations, what I need to know is why suddenly when loading the scene it breaks. If someone has any idea or can give some information, anything will be appreciated
And if this has to do with the code I can upload it…
Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...);
statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
I identified the problem but I have no idea on how to fix it, I already tried multiple things such as lowering the overlap circle radius, debugging this piece of code, touching around the colliders, The OnLanding event is being called when the character jumps, which i’m assuming it detects instantly after pressing Space.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
OnLandEvent is a UnityEvent associated with the OnLanding() Function. So when the OnLandEvent is invoked, OnLanding is called. The content on the OnLanding Function is just the bool of IsJumping being set to false (Animator Component SetBool function), for the animation to stop playing.
Before you tear your code up too much you might want to prove that is what’s happening instead of assuming.
But if it is, there’s a few ways to deal with this:
- have a separate lower “ground check” overlap and don’t check it unless your motion is downwards
OR
- inhibit checking for a short time after jumping (eg, just set a float timer to 0.1f seconds and count it down)
OR
- other ways to might find on various youtube tutorials on 2D character controllers that jump
1 Like
Yeah, checking only when the motion is downwards worked, thank you for your time!