- Home /
Check if 2D Character is grounded
To do this i looked a lot on the internet. I tried to check the velocity, but checking 2d velocity doesn't seem to work for some reason. But what i found was i should probably try linecast. But all the things online were all C# which i do not want. Is there like a simple string of code i can put into a script to check if the attached gameobject is grounded? Easiest method please haha
Here is an article I found which explains how to use Physics.OverlapCircle to check if object is grounded. $$anonymous$$aybe it might help someone who is struggling to get this
MBo does a walk through of setting up a 2D player controller and around the 12:15 mark he does a step by step on the isGrounded step(s).
for those who require a visual explanation.
Answer by Josh707 · May 15, 2014 at 07:45 AM
I think you will want to use Physics2D.OverlapArea rather than a raycast at a single point. I can guarantee a raycast would end up with you sitting on the corner of your collider and it doesn't say you're grounded. Basically you can check a rectangular area for collisions and use that to determine if you're grounded.
//Corners of the rectangle to check
public var top_left : Transform;
public var bottom_right : Transform;
public var ground_layers : LayerMask;
var grounded : bool;
function FixedUpdate(){
grounded = Physics2D.OverlapArea(top_left.position, bottom_right.position, ground_layers);
}
Depending on the shape of your characters collider you might want to use Physics2D.OverlapCircle instead.
Uh sorry, but how do i set transforms to the corner of an object? Should i put blank gameobjects with colliders and parent it? Once again i'm sorry I am a noob and i really appreciate the help.
It's alright! No, you don't need any colliders or anything, just 2 empty GameObjects which are a child of your sprite/character. OverlapArea takes 2 positions to make a rectangle, which should be opposite corners. You don't actually need 2 GameObjects for this but it makes it quicker to tweak.
You are flippin awesome dude, i was so intent on getting raycast to work i didn't even think of a different easier solution. Thanks a lot it helps so much
Haha no problem! Also I just read OverlapArea allocates memory every time it is called so you should check out OverlapAreaNonAlloc ins$$anonymous$$d, since it's being called very frequently.
@Josh707 @matta9001 OverlapAerea return a collider not a bool... the code is not working..
Answer by siddharth3322 · May 14, 2014 at 05:21 AM
Basically if you are developing character based game then you must have to view this video. As per my consideration you definitely find your all doubts clear.
Problem is its C#, im sure i will be able to pick some stuff out though i hope.
Answer by Slulego · May 14, 2014 at 03:37 PM
I think you want to raycast. Try adding a raycast pointing down to check if something is below it.
If it shows up as C#, there should be a drop down near the top left that says "Scripting Reference Using-dropdown"
function FixedUpdate() {
// Cast a ray straight down.
var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up, 0.1);
// If it hits something...
if (hit != null) {
isOnGround = true;
}
}
Hope this helps.
See heres the thing, the code you gave me just fires a function when the raycast hits something. Do you you know how to calculate how long that raycast is and i can make a < or > if statement on the value? So i can make it like if(distance > .1){ isGrounded = true; } any idaes?
I really hate saying this but...this doesn't work either. I put the character up in the air and a debug.log to see if the boolean changed but...it doesn't :(, i put an else statement to see if it would change but i couldn't get it to work. I'm not a coding genius and im tryin all i can. I just want it so that if its touching the ground, isGrounded = true, and if its not, than isGrounded = false, i can handle all the other scripting with just that boolean but this is literally the only issue ive come across. I dunno what it is it just doesn't seem to work.
The sample code in the documentation is incorrect on this point. "if (hit != null)" is useless code because Raycast ALWAYS returns an object, it does not return null.
I'm not entirely clear on the JavaScript implementation, but in C# you can simply use "if (hit)" to accomplish that check correctly. If that doesn't work, check the RaycastHit2D's collider; if it's null, there's no hit. So, "if (hit.collider != null)".
Interestingly, this is correctly documented (without sample code) here:
$$anonymous$$aybe try
if(hit != null && hit.transform != null)
Also I'm not sure if you have background sprites and whether that will effect it any.
Answer by Zachary Hoffman · May 15, 2014 at 03:06 AM
Add a character controller and check the .isgrounded property of it.
Here is the Unityscript reference
https://docs.unity3d.com/Documentation/ScriptReference/CharacterController-isGrounded.html
Here is JavaScript
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
print("We are grounded");
}
NOTE: Must add a charactercontroller to your object for this to work
character controller is 3d mate, im making a 2d sprite controller
Answer by Aatifahmad · Oct 01, 2019 at 04:40 PM
If raycast does not work! use this.
private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "platForm") { isOnGround = true; } }
private void OnCollisionExit2D(Collision2D col)
{
isOnGround = false;
}