Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by matta9001 · May 14, 2014 at 04:31 AM · 2djavascript

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image falconer · Nov 15, 2014 at 07:50 PM 0
Share

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

Check Object Grounded

avatar image troynall · May 15 at 01:10 AM 0
Share

https://youtu.be/9HAZQROH2gM

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.

5 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matta9001 · May 15, 2014 at 12:53 PM 0
Share

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.

avatar image Josh707 · May 15, 2014 at 02:19 PM 0
Share

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.

avatar image matta9001 · May 16, 2014 at 06:11 AM 0
Share

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

avatar image Josh707 · May 16, 2014 at 07:38 PM 0
Share

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.

avatar image LeleUnity · Jun 04, 2020 at 08:37 PM 0
Share

@Josh707 @matta9001 OverlapAerea return a collider not a bool... the code is not working..

avatar image
2

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.

2D Character Controller

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matta9001 · May 14, 2014 at 12:18 PM 0
Share

Problem is its C#, im sure i will be able to pick some stuff out though i hope.

avatar image
2
Wiki

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.

Physics2D.Raycast

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matta9001 · May 14, 2014 at 08:54 PM 0
Share

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?

avatar image Slulego · May 15, 2014 at 02:48 AM 0
Share

Oops, my bad. Updated.

avatar image matta9001 · May 15, 2014 at 03:04 AM 0
Share

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.

avatar image Pyrian · May 15, 2014 at 05:11 AM 0
Share

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:

RaycastHit2D.collider

avatar image Slulego · May 17, 2014 at 02:41 PM 0
Share

$$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.

avatar image
-1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matta9001 · May 15, 2014 at 03:56 AM 0
Share

character controller is 3d mate, im making a 2d sprite controller

avatar image
-1

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to make a function stop when all the Gameobjects from the array are in the Game? 1 Answer

Uni2D Play() - Misunderstanding 3 Answers

Particle System On Key Press 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges