Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 daveoncode · Jul 06, 2016 at 05:48 PM · 2draycastphysics2draycasthit2d

Unity Physics2D.Raycast always returns “the invoker” as hit

I'm trying to use Physics2D.Raycast in order to check if the player is on the ground (I know that there are other approaches to check if the player is on the ground but I think that the raycast is the most reliable). The problem is that in my scenario it returns the player itself as hit and I really don't understand why and what should I do. My code (in PlayerController) is the following:

 public bool IsGrounded () {
         Bounds bounds = this.playerCollider.bounds;
         Vector3 rayOrigin = bounds.center;
         rayOrigin.y -= bounds.extents.y;
         RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.down, 0.1f);
         if (hit.collider != null) {
             Debug.Log ("Collider is: " + hit.collider.name);
         }
         return hit.collider != null;
     }

And I can debug the casted ray using:

 Debug.DrawLine (rayOrigin, new Vector3 (rayOrigin.x, rayOrigin.y - 0.1f, rayOrigin.z), Color.magenta);

...and it gets casted as expected, while the Debug.Log always reports "Player" which is itself and I don't know how it's possible. So what's wrong?

ps. I'm using Unity 5.3

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Jul 06, 2016 at 09:51 PM

Simplest solution is to pass a LayerMask to the raycast that excludes the layer that the player is in.

Another more complex solution is to use RayCastAll, then sort the array of RayCastHits by distance, and then loop through the array, ingoring any hit that is the player object, and stopping once the first "real" hit is reached.

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
avatar image
0

Answer by milanow · Jul 06, 2016 at 10:12 PM

The problem is that the ray always hits the player since you set the ray's startPoint as bound's center. Which is to say ray's start point "hits" the player's collision box first. And hit.collider always return the first object it hits. That's why you can never access to the "ground" object.

Cherno's answer is the best, you can change the layermask of the ray to cast ray on different layer. Simply separate player and ground to different layers and cast ray on ground's layer. Or as he said using raycastAll.

And a little tricky answer is moving the ray's original start point a little bit far from the bound (like a yOffest = 0.1f).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Layer Mask on raycast2d not working? 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

Raycast2D will point right, but won't point left when player turns. 1 Answer

2D Raycast effect only what it hits? 2 Answers

Raycast for a 2D objects 0 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