Switch statements

alright so i came back here to ask how do i switch my switch statement into if statement because it looks like in a switch statement you cant check if something is false and use else for example in my code im trying to check if my cube is touching the ground then i can jump. but i cant put an else statement so as soon as it touches the ground i can keep pressing space to jump infinitely after touching the ground once. how do i check for it if its not touching the ground?

private void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "Ground":
                IsGrounded = true; 
                break;
            
        }
    }

Ive recently found it how to do it but its starting to seem like you cant try and code this way using tags because when the object collides with something and then makes a statement true then even after being uncollided from it it will still be true and it will only return false if it collides with something other than what i called “Ground”. not sure if it makes sense but i made my boolean public to show you what i mean.

my code is this.

 private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            IsGrounded = true;
        } else if(collision.gameObject.tag != "Ground")
        {
            IsGrounded = false;
        }
    }

If you need to handle the states in this way you might consider using OnCollisionExit.

Please also apply code formatting when copy/pasting your code Jayy, it makes it a lot easier for everyone to read.

Hope this helps :slight_smile:


See also;

1 Like

the only way that would work is if after colliding with the ground tag i get off of it and after coming off of it the boolean still remains true.

i would make a collision enter and exit but idk if thats a good thing to do. so after the collision enter i make another one where its oncillisionexit and change the boolean to false? seems like it could work

Potentially yes, but as I don’t know what it is you are actually trying to achieve its a bit hard to give anything more definitive as a solution.

Im just trying to jump once after pressing space instead of every time i press it. I just tried it. It worked with the collision exit instead of an else statement.

MY CODE.
…
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == “Ground”)
{
IsGrounded = true;
}
}

private void OnCollisionExit(Collision collision)
{
    if (collision.gameObject.tag == "Ground")
    {
        IsGrounded = false;
    }
}

…

Im basically just trying to test myself on my knowledge throughout the course without watching videos and just taking what i know and have from the videos i know that maybe doing things like this is inconsistent because for example if i was to walk off the edge and hug the object i could climb it pressing space since im colliding with it but the onCollisionExit helped. Thanks Rob.

You’re welcome, please do add code formatting to your posts when cooy/pasting code, link above. Thanks.

I followed the instructions and did the three … before and after and it didnt seem to work idk why.

Wrong character, as per the link I gave you before, its ` not .. On my keyboard, this is the key to the left of the number one key on the main keyboard.

Hope this helps :slight_smile:

Ahhh ok i wouldnt have guessed that lol thanks.

1 Like

That’s why I gave you the link to the Guide :wink:

Incidentally, if you find it easier, you can also use the </> icon on the composer tool bar, if you hover over it it will say “Preformatted Text”, this is configured to apply the code fencing around whatever is selected. For little snippets this may be easier, for longer scripts, or multiple scripts in the same post I tend to personally find adding the 3 characters manually a bit more straight forward as it doesn’t require any scrolling with the selection etc. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms