• 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 Dunkelheit · Nov 28, 2013 at 03:13 AM · 2dplatformgroundrigid

Check if the player is on the ground. Unity4.3 (2D)

I'm checking the new 2D features in Unity 4.3 edition. So they changed some components to work now with 2D environment. I'm using Physics2D.Linecast to check if player is on the ground. But everywhere my char stay on ground, even if it's jumping. My ground is marked as ground to get correct layer to compare char vector with the ground's one.

 grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

  • "grounded" is a bool to check if player is on the ground;

  • "transform" refers player's transform;

  • "groundCheck" is an emptyObject put into player to mark ground directions.

Since I'm using the new resources, probably raycast or "old" components to handle 3D environments can't help me there.

Thanks in advance and sorry about my english.

Comment
Add comment · Show 1
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:51 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

1 Reply

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

Answer by diegzumillo · Nov 28, 2013 at 04:37 AM

You need to use Vector2 as arguments for the Physics2D.Linecast. I made the same mistake when migrating to 2D physics :P Something like

 var MyPos : Vector2 = Vector2(transform.position.x, transform.position.y);
 var GroundCheckPos : Vector2 = Vector2(groundCheck.position.x, groundCheck.position.y);
 Physics2D.Linecast(MyPos , GroundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
Comment
Add comment · Show 7 · 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 Dunkelheit · Nov 28, 2013 at 12:24 PM 0
Share

Thanks so much, diegzumillo. I've changed the code and worked now.

 Vector2 playerPos = new Vector2(transform.position.x, transform.position.y);
 Vector2 groundPos = new Vector2(groundCheck.position.x, groundCheck.position.y);
 
 grounded = Physics2D.Linecast(playerPos, groundPos, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
avatar image Dunkelheit · Nov 28, 2013 at 12:26 PM 0
Share

I've copied the base code from an official example of 2D Platform game and the weird behavior is there the game works as well, I needed to change based from your snippet code to make $$anonymous$$e running. Thanks

avatar image KernelSys · Apr 07, 2014 at 04:27 PM 0
Share

Excuse me, but what is the use of 1 << in 1 << Layer$$anonymous$$ask.NameToLayer("Ground")? Any explanation would be appreciated.

avatar image perchik · Apr 07, 2014 at 04:30 PM 0
Share

the << is a bit shifting operator. The layer$$anonymous$$ask is just an integer, but the way the different layers are handled are in binary. So the layermask might look like 1010110 but the integer it's stored as will be "86"

avatar image diegzumillo · Apr 07, 2014 at 04:33 PM 0
Share

Hi $$anonymous$$ernelSys, read the manual on layers to learn about combining layers, here's the link: http://docs.unity3d.com/Documentation/Components/Layers.html

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Going through a moving Platform? 0 Answers

Unity 2D Platformer: How to properly implement this ground check? 1 Answer

2D Animation does not start 1 Answer

How to make a 2D physical movement like this ? 0 Answers

How to change only 1 game object 1 Answer


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