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 seanleongkw · Mar 17, 2017 at 03:28 PM · 2draycast2d-platformerraycastingraycasthit

Physics.Raycast doesn't hit anything even though Debug.DrawRay works

I'm trying to figure out if a player is on top of any platform tiles, which are made in the script from prefabs.

 bool isGrounded(){
         Debug.DrawRay (transform.position, -Vector3.up * (collider2D.bounds.size.y / 2 + 0.1f), Color.red, 1000);
         RaycastHit h = new RaycastHit ();
         return Physics.Raycast (transform.position, -Vector3.up, out h, collider2D.bounds.size.y / 2 + 0.1f);
     }


Debug.DrawRay draws a ray into the platform tiles, but Physics.Raycast doesn't seem to detect that there's anything there.

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 seanleongkw · Mar 17, 2017 at 05:46 PM 0
Share

EDIT: So I tried using Raycast2D but it still doesn't detect anything and throws a NullReferenceException.

 bool isGrounded(){
         Debug.DrawRay (new Vector3(transform.position.x, transform.position.y - collider2D.bounds.size.y / 2 - 0.01f, 0), -Vector3.up * 0.1f, Color.red, 1000);
         string name = Physics2D.Raycast (new Vector2(transform.position.x, transform.position.y - collider2D.bounds.size.y / 2 - 0.01f), -Vector2.up, 0.1f, 8).collider.gameObject.name;
         return true;//name.Contains ("wood") || name == "wallBottom";
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
10

Answer by Commoble · Mar 17, 2017 at 03:31 PM

You need to use Physics2D.Raycast to detect 2D colliders.

Comment
Add comment · Show 4 · 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 seanleongkw · Mar 17, 2017 at 04:32 PM 0
Share

It still doesn't work.

 bool isGrounded(){
         Debug.DrawRay (transform.position, -Vector3.up * (collider2D.bounds.size.y / 2 + 0.1f), Color.red, 1000);
         string name = Physics2D.Raycast (transform.position, -Vector3.up, collider2D.bounds.size.y / 2 + 0.1f, 8).collider.gameObject.name;
         return name.Contains ("wood") || name == "wallBottom";
     }

It just says that there is a NullReferenceException. It doesn't hit anything. Before I added the layer$$anonymous$$ask, the ray only hit the player itself.

avatar image Commoble seanleongkw · Mar 17, 2017 at 06:15 PM 2
Share

It's not hitting anything because no objects in your scene exist in that Layer$$anonymous$$ask's layers.

A Layer$$anonymous$$ask isn't the same thing as a layer ID. A layer mask that only includes layer 8 would have a value of 256; likewise, a layer mask with a value of 8 would only include Builtin Layer 3, which is unused and cannot be used. A good way to use layermasks is to declare a public Layer$$anonymous$$ask groundlayers; field in your script, and set the relevant layers in the Inspector.

avatar image seanleongkw Commoble · Mar 18, 2017 at 01:13 AM 0
Share

It works now, thank you so much!

avatar image kyliant · Mar 30, 2019 at 11:19 PM 0
Share

I think you just saved my sanity.

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

6 People are following this question.

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

Related Questions

RaycastHit.point Collider issue C# 0 Answers

Ray is not being detected 0 Answers

raycastHit.point - How do I make it ignore colliders? 1 Answer

How to find an object in front of player? 1 Answer

RayCast can't detect GameObject at Player's feet 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