Issues checking if player is grounded

Hello! I’m learning 2D platformer game concepts and I’m struggled with grounding. The issue is when the player jumps the ground check is immediately triggered. I am using a third-party character controller that uses a circlecast to determine if the player has collided with the ground.

private void FixedUpdate()
    {
        bool wasGrounded = m_Grounded;
        m_Grounded = false;

        // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
        // This can be done using layers instead but Sample Assets will not overwrite your project settings.
        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();
            }
        }
    }

I thought of adding in a velocity check before invoking the OnLandEvent such that we only invoke that method if the player’s velocity is near-zero. Would this be an appropriate way to solve this issue? I’ve attempted this but it seems that the velocity check is close enough to zero to trigger the event meaning fixedUpdate is called so close to the actual jump event that we have not yet given the player velocity on the y-axis.

Are there any other best practices for solving this issue? I’ve looked around and seen some people suggest adding a timer that starts when the player jumps and only invoking OnLandEvent after some amount of time has passed, but this seems hackish to me and I would prefer to not to make this time-based.

Thanks in advance!

There are a few issues, you may want to go back and watch the video or brush up on some basic C# things. But, i will help to explain them.

First off, most of your issues arise from putting too much into the FixedUpdate function. The FixedUpdate function is very similar to Update as they occur every frame. FixedUpdate is a fixed amount of time and Update is framerate dependent. Regardless, you are doing all of that code within the FixedUpdate (aka, you are saying grounded is always false).

You should move your bool declaration: wasGrounded outside of FixedUpdate to make it a global variable, currently it is a local one. I dont know where m_grounded is declared but you are setting it to false in FixedUpdate like i mentioned above, not good and i dont know of any application where that would be used. Place that in Start or Awake most likely.

Also, you are calling a For loop inside of FixedUpdate which is going to hurt performance if it doesnt just freeze your application entirely.

I would just follow a good tutorial like this to help you understand these better:

Hi! Thanks for your reply. I’ve actually worked through that video previously and I’m currently working through a course that has provided the character controller code. I am trying to modify it because like you said there are definitely a few issues there. I am new to C# but I am a professional software engineer with extensive OOP experience in other languages, so it’s mainly just a matter of learning about C# paradigms and design patterns and how they’re used with Unity. I appreciate your description of some of the concepts here.

I believe this character controller is actually the same one included in Brackey’s video though and he doesn’t go in-depth into the scripting itself (nor does the course I’m working on now). In short - Brackey’s tutorial is great but it’s not particularly helpful at this point in my learning.

Do you know of any other solid in-depth resources to learn about character movement from a scripting perspective? I have referenced Brackey’s videos, Unity documentation, and a Udemy course I’m currently working through.

For sure. Here is an older Brackey’s tutorial vid that is actually part of a long 2D platformer series. This video is basically what i use for my platformer now. It is very similar to the one i shared above but he goes more into the scripting.

Here is another video that may be helpful:

And here is my handy-dandy list of Unity subs on YouTube that have all helped me in one way or another:

6488793--729396--upload_2020-11-3_20-42-1.png

My favorites are (in no particular order): Brackeys, GamesPlusJames, GameGrind, Jason Weimann, BlackThornProd, and Hamza Herbou.

For some reference: Brackeys old videos are more tutorial series and his newer ones are more feature based, some of which require an Asset Store purchase.

GamePlusJames, GameGrind and Blackthornprod all are great at doing full Unity tutorials and do have their fair share of random feature vids too.

Jason Weimann is great for understanding Unity better and how to structure games properly. He doesnt go line by line like some of the others but still shows the code and then explains it, and then shows an in-game example.

Then Hamza is a newer one i found who has some awesome back-end videos like using a real-time counter (think adding lives every hour, even if app closed) and putting ads into your videos. He doesnt have many subs right now but his videos are short, to the point and have helped me a ton.

Hope this helps!

Thanks so much! I appreciate that a ton. Good to know to dig into some of his old videos for a more in-depth technical look at some of these concepts.