#Grounding

1 messages · Page 1 of 1 (latest)

somber kernel
#

ah, you had it the other way around. Let me explain in a thread.

devout pulsar
#

ok

somber kernel
#

you keep your original script

#

and attach this script to the same object
**Edit: **I mean the Ground Check script I linked, not the one you modified

#

and in your original script, you need to add some lines

#

add a private variable with Type as the script's name

in Awake, set it with GetComponent<ScriptName>();

#

Once that's done. You can reference the isGrounded bool by variableName.isGrounded

#

---- Alternatively you can bake my original script into your own, but it's much more clean this way

devout pulsar
#

Like this?

        {
            //Gives a Player object velocity on +Y axis.
            rb.velocity += new Vector2(0, jumpForce);
        }```
somber kernel
#

Yes, precisely

devout pulsar
#

ok I will try this but first I have to fix something because now I can't jump at all xd

#

Fixed, the value was set at 1

#

But should I remove the if (!bc.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }

#

from my OnJump

#

?

somber kernel
#

yes

#

this replaces that

devout pulsar
#

Well now my player can jump in the air

somber kernel
#

post code?

#

is the Ground Script attached to the object?

devout pulsar
#

ye

#

It tells me there is error with this line

#

ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: Cannot get contact at index 0. There are 0 contact(s).

somber kernel
#

Post code for that entire script

devout pulsar
#

Player code

devout pulsar
#

here

somber kernel
#

are you certain that is 100% the code you're running?

#

'cause if it is, then you either have a special case (maybe Restart Unity)
or I've been sharing Wrong Code for months

#

but no one else have complained about errors

devout pulsar
#

Ye I have both scripts in the Player object

somber kernel
#

and they are the correct scripts?

devout pulsar
#

The 2 I just sent

#

I will try restarting

somber kernel
#

Ok

#

Btw, I see that I misread the first code link you sent in this thread

#

//And then on my OnJump script

#

it was correct, just needed to remove the other GetMask Ground thing

devout pulsar
#

but where

#

In my or your script

somber kernel
#

the script I linked stays the way it is - you just read the Public Bool

#

from your player script

#

wait a minute

#

the player script you just posted

#

it doesn't contain any reference to the ground check
and you've added a

if (!Physics2D.OverlapBox((Vector2)transform.position + groundCheckPosition, groundCheckSize, 0, groundLayers))
        {
            return;
        }
devout pulsar
#

Do I add this?

somber kernel
#

No, read what I'm typing out.

#

You're doing multiple things. I'm not sure you've got your head around this.

devout pulsar
#

probably not

#

I had this line before ``` if (bc.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
return;
}

#

But I thought that would mess with the script you gave me

somber kernel
#

From the code you posted, and the instructions I gave, the path is simple.
You add the GroundCheck script to the same GameObject as the PlayerScript is attached.
Then you make a variable in your Player script to hold a reference to the GroundCheck

  • and use GetComponent in Awake
    And then you remove your original Jump's internal ground check
if (!bc.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }

and implement the new one by only allowing jump if isGrounded == true

#

Have you referenced values between scripts before?

devout pulsar
#

Wait

#

I sent you wrong player script

#

this is the one

somber kernel
#
private MyScriptName variableName;

void Awake()
{
    variableName = GetComponent<MyScriptName>(); // Attached to the game object
}

void Jump()
{
    if(variableName.isGrounded == true) // jump
}
devout pulsar
#

is there a difference if I do it with void Awake or void Start?

somber kernel
#

where do you call OnJump() ?

devout pulsar
#

I dont. The class OnJump is called when certain input is used

#

In my case its Space

somber kernel
#

and when do you get the aforementioned error?

somber kernel
devout pulsar
#

Whenever I jump I get ```ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: Cannot get contact at index 0. There are 0 contact(s).
UnityEngine.Collision2D.GetContact (System.Int32 index) (at <b0e795e48cf04d7a9193ea9447af406a>:0)
GroundCheck.OnCollisionExit2D (UnityEngine.Collision2D collision) (at Assets/Scripts/GroundCheck.cs:42)

somber kernel
#

words shape reality
it's important to use the right words, so you can think clearly

#

nothing to be sorry about

#

can't know what you haven't learned

devout pulsar
#

Ye I just started not even a half year ago

#

Vector2 contact = collision.GetContact(0).normal; but this is the line with an error

#

according to the console

somber kernel
#

is your collider enabled in the inspector?

#

is it ever disabled during code?

devout pulsar
#

I don't think so

#
    {   
       
        if (!notDead)
        {
            return;
        }
        if (gc.isGrounded == true)
        {
            //Gives a Player object velocity on +Y axis.
            rb.velocity += new Vector2(0, jumpForce);
        }```
somber kernel
#

collapse all the components of the player object, and screenshot so I can see all the components

#

Are you mixing 2D and 3D colliders in the game project?

devout pulsar
#

I hope not, but let me check

#

no, everything is in 2D

somber kernel
#

go to your tilemap and show me the type of collider its using

devout pulsar
somber kernel
#

Have you enabled this ?

Used by Composite

devout pulsar
#

yes

somber kernel
#

hmm

#

there is one thing that is weird

#

You say the error comes when you Press the Jump button

devout pulsar
#
    {
        Vector2 contact = collision.GetContact(0).normal;

        if (collision.collider.gameObject.layer == groundLayer)
        {
            if (contact.y > 0)
            {
                isGrounded = true;
            }
        }
    }```
when  the value of contact.y > 0 I can always jump but when I change it to 1 I can't jump at all
somber kernel
#

why would you change it to 1 ?

devout pulsar
#

just to check if its even working

#

bc you wasn't sure if I connected it to the object corretly

somber kernel
#

kk

#

Add a Debug.Log("Enter") and Exit to the Collision events

devout pulsar
#

Now I get 2 errors acutally

#
UnityEditor.GameObjectInspector.ClearPreviewCache () (at <ef3b6bf002d8435a97b4e938f6c49b02>:0)
UnityEditor.GameObjectInspector.OnDisable () (at <ef3b6bf002d8435a97b4e938f6c49b02>:0)
UnityEditor.Tilemaps.GridPaintPaletteClipboard:OnDisable() (at C:/Users/joel.gylden/Desktop/Elevprojekt Examination #1/Sidescroller - SU21a/Library/PackageCache/com.unity.2d.tilemap@1.0.0/Editor/GridPaintPaletteClipboard.cs:347)

somber kernel
#

then you've changed something I know nothing about, or Unity is derping

devout pulsar
#

Ok

#

so

#

I get the Debug.Log on the first part of the code

#
    {
        Vector2 contact = collision.GetContact(0).normal;

        if (collision.collider.gameObject.layer == groundLayer)
        {
            if (contact.y > 0)
            {
                Debug.Log("ON");
                isGrounded = true;
            }
        }
    }
somber kernel
#

and the exit?

devout pulsar
#

nothing from the exit

somber kernel
#

is the player able to Jump?

devout pulsar
#

Ye

#

but infinitly

somber kernel
#

and when it leaves ground, the error displays?

devout pulsar
#

Ye

somber kernel
#

or every time you press Jump?

devout pulsar
#

when it leaves ground

somber kernel
#

huh, try this

#

instead of making GroundCheck a separate script

#

add the code to the main script
make isGrounded private

#

see if that works

devout pulsar
#

but havent I done it before?

somber kernel
#

I haven't told you to do that before.

#

so idk

devout pulsar
#

Ye but I did that by mistake

somber kernel
#

was it correctly implemented?

devout pulsar
#

probably not let me do it again

somber kernel
#

Actually, show me your PlayerInput component. Is it a script or an asset?

devout pulsar
somber kernel
#

Where did you get this?

devout pulsar
#

my teacher

somber kernel
#

is it the new input system?

devout pulsar
#

I have no idea

somber kernel
#

Has he hinted at which approach to use for grounding?

#

were you supposed to use the one you previously had?

devout pulsar
#

What we did in our orginall project, we used separete boxCollider2D

#

as a groundCheck

somber kernel
#

and now you were told to do it in a scriptable way?

devout pulsar
#

yes

#

I mean

#

I wasn't told to do that but to do similar player properties using only script

somber kernel
#

Well. I'm not sure why OnCollisionExit2D is not working

#

so just go with a Cast

#

like the other guy suggested

#

✅ Get the Project files and Utilities at https://unitycodemonkey.com/video.php?v=c3iEl5AwUF8
Let's look at 3 different methods for doing a Ground Check which is a necessity if you're making a Platformer.

If you have any questions post them in the comments and I'll do my best to answer them.

🔔 Subscribe for more Unity Tutorials https://www.yout...

â–¶ Play video
#

BoxCast is good for BoxColliders, unless you want special quirks, then go with multiple Raycasts

#

you should be able to get the surface normal (a directional vector) from the point of contact

#

but you can also just shape it to only work with ground

devout pulsar
#

I would but the problem with this, is that I can't add any more components to the player

#

I guess I will have to skip it

#

But thank you for your effort

somber kernel
#

a BoxCast isn't a component

#

it is code

#

you can visualize it with Gizmos or Debug.Draw something

#

Good luck o/