#Grounding
1 messages · Page 1 of 1 (latest)
ok
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
Your original code - https://gdl.space/dukodobuju.cs
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
Like this?
{
//Gives a Player object velocity on +Y axis.
rb.velocity += new Vector2(0, jumpForce);
}```
Yes, precisely
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
?
Well now my player can jump in the air
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).
the script with this line
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
Ye I have both scripts in the Player object
and they are the correct scripts?
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
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;
}
Do I add this?
No, read what I'm typing out.
You're doing multiple things. I'm not sure you've got your head around this.
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
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?
private MyScriptName variableName;
void Awake()
{
variableName = GetComponent<MyScriptName>(); // Attached to the game object
}
void Jump()
{
if(variableName.isGrounded == true) // jump
}
is there a difference if I do it with void Awake or void Start?
where do you call OnJump() ?
I dont. The class OnJump is called when certain input is used
In my case its Space
Awake and OnEnable are called on all Game Objects, before Start is ever called
and when do you get the aforementioned error?
function/method, not class. Player is the class. (details)
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)
oh sorry
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
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
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);
}```
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?
go to your tilemap and show me the type of collider its using
Have you enabled this ?
Used by Composite
yes
hmm
there is one thing that is weird
You say the error comes when you Press the Jump button
{
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
why would you change it to 1 ?
just to check if its even working
bc you wasn't sure if I connected it to the object corretly
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)
then you've changed something I know nothing about, or Unity is derping
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;
}
}
}
and the exit?
nothing from the exit
is the player able to Jump?
and when it leaves ground, the error displays?
Ye
or every time you press Jump?
when it leaves ground
huh, try this
instead of making GroundCheck a separate script
add the code to the main script
make isGrounded private
see if that works
but havent I done it before?
Ye but I did that by mistake
was it correctly implemented?
probably not let me do it again
Actually, show me your PlayerInput component. Is it a script or an asset?
Where did you get this?
my teacher
is it the new input system?
I have no idea
Has he hinted at which approach to use for grounding?
were you supposed to use the one you previously had?
What we did in our orginall project, we used separete boxCollider2D
as a groundCheck
and now you were told to do it in a scriptable way?
yes
I mean
I wasn't told to do that but to do similar player properties using only script
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...
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