Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SK_Ren · Jul 01, 2015 at 10:36 AM · 2dphysics2dlayermaskgrounded

Physics2D.Raycast is fine, I'm an idiot

Currently trying to get a ground hit detection system working with Raycast(). This is my current progress:

 public bool isGrounded {
             get{
                 LayerMask mask = (1 << LayerMask.NameToLayer ("Terrain"));
     
                 RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector3.down, 2, mask);
                 Debug.DrawRay (transform.position, Vector3.down, Color.green);

                 // If RaycastHit2D.collider is not Null
                 if (hit.collider != null) {
                     Debug.Log ("Raycast Collider not Null");

                     // Check max grounded distance
                     if (Vector2.Distance (hit.point, transform.position) < 0.5f) {
                         Debug.Log ("Raycast Collider Tag: " + hit.collider.tag);

                         // Return whether the hit collider is a Platform or not
                         return hit.collider.CompareTag ("Platform");
                     } 

                     // Player is not grounded, return false.
                     else {
                         Debug.Log ("Player is not Grounded");
                         return false;
                     } 
                 } else {
                     Debug.Log("Collider is Null or Did not hit anything");
                     return false;
                 }
             }
         }


The intended use for this is to be called in one of three locations to check if the player is grounded:

  1. The Player presses Jump

  2. The player has non-0 y velocity and is currently marked as grounded

  3. The player has 0 y velocity and is currently marked as not grounded

Not a fool-proof system, but it should handle most cases while I work on other things. My issue is that the Raycast always returns the player object as being hit despite it being on a layer different from the one specified. And if I switch my Player object to Layer:IgnoreRaycast, the Raycast hits nothing despite there being a large Edge Collider 2D below my character. I've tried substituting it with a box collider as well and it still isn't seen by the Raycast(). All of my platforms are marked as Layer:Terrain, Tag:Platform.


OK, I figured out my issue. Aside from overusing the term Player and getting really confused, I forgot to account for the size offset of the Player's boxcollider2D.

Comment
Add comment · Show 3
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 meat5000 ♦ · Jul 01, 2015 at 11:11 AM 0
Share

Have you actually added "Terrain" to the Layers list?

NameToLayer searches the Layers list, not the hierarchy or tags.

avatar image SK_Ren · Jul 01, 2015 at 11:32 AM 0
Share

Yes, I have added "Terrain" to the layers list and set all my platforms to be on that layer. Likewise the Player object is on the "Player" layer.

avatar image Cherno · Jul 01, 2015 at 12:34 PM 0
Share

You could try a RaycastAll, sort the hits by distance, iterate through the array, continue if the hit.collider is the player, and break on the first occurance of a hit terrain.

 using System.Linq;
 
 RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction, 100f, layer$$anonymous$$ask, -$$anonymous$$athf.Infinity, $$anonymous$$athf.Infinity).OrderBy(h=>h.distance).ToArray();
                 foreach(RaycastHit2D hit in hits) {
                     if(hit.collider.gameObject != gameObject) {
                         break;
                     }
                 }

2 Replies

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

Answer by meat5000 · Jul 01, 2015 at 01:00 PM

I would Debug.Log hit.point to see where this false detection is occuring. It might show an inconsistency enough to determine that OP has a forgotten script at work or something to that effect.

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 SK_Ren · Jul 01, 2015 at 05:24 PM 0
Share

Thank you meat5000. Doing that I found that the ray was in fact hitting the platform, the error was in my placement of Log messages and using Player in too many cases.

The actual problem it seems was that somewhere along the line I stopped accounting for the Boxcollider size when checking distance to see if the player was grounded. So it was always too far away. Thanks everyone. I guess I was just missing something simple.I just added

 float offset = (GetComponent<BoxCollider2D>().size.y / 2);

and added it to the distance check.

avatar image
1

Answer by Dave-Carlile · Jul 01, 2015 at 12:46 PM

LayerMask.NameToLayer returns the layer index. You need to pass a Layer Mask to Raycast, so use LayerMask.GetMask.

Edit: Upon rereading your code, it looks like you're using the index to create the mask. Seems like that should work too, but try using GetMask to see if that helps.

Have you tried increasing the max ray length?

Comment
Add comment · Show 2 · 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 meat5000 ♦ · Jul 01, 2015 at 12:50 PM 0
Share

$$anonymous$$y issue is that the Raycast always returns the player object as being hit despite it being on a layer different from the one specified

This is the key

avatar image Dave-Carlile · Jul 01, 2015 at 12:53 PM 0
Share

What's odd is that the raycast isn't supposed to return a hit for a collider if the ray origin is inside that collider.

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

Bitmask/LayerMask used in raycast not working 2 Answers

How to walk on 2d sphere (planet) 2 Answers

Triying to make a character take knockback in a parabolic motion in 2D. 0 Answers

While Moving Left or Right my character falls more slowly. 2 Answers

Assigning found enemy to target variable 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