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 daniel14657 · Sep 27, 2020 at 08:04 AM · physics.raycast

Player doesn't detect ground by using Raycast

Hey, For some reason my Player doesn't detect the ground properly, I can't find the issue or why is it happening but it has to be something with the layer,And I already checked. There's a layer called Ground. Here is my code:

     // If the Player is Grounded
     bool Is_Grounded = Physics2D.Raycast(transform.position, transform.position - (new Vector3(0, 0.6f)), -0.6f, LayerMask.NameToLayer("Ground"));
     Debug.DrawLine(transform.position, transform.position - (new Vector3(0, 0.6f)), Color.black, 1f);
     if (Is_Grounded == true)
     {
         Debug.Log("Grounded");
     }

     if( Is_Grounded == false)
     {
         Debug.Log("Not Grounded");
     }
 

Thanks for the help

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by homer_3 · Sep 27, 2020 at 01:43 PM

Change "LayerMask.NameToLayer("Ground")" to "1 << LayerMask.NameToLayer("Ground")".

Comment
Add comment · Show 3 · 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 daniel14657 · Sep 27, 2020 at 03:44 PM 0
Share

Thanks for your advice, unfortunately, I already tried that and it kind of worked... Well, It worked as long as I stayed in the spawn position, As soon as I move in any direction it won't detect the ground any more for some reason

avatar image homer_3 daniel14657 · Sep 27, 2020 at 05:47 PM 0
Share

Your 2nd argument to Raycast is also kind of weird, which could be causing you issues. It's actually saying to cast the ray away from that fixed point. I'd use Vector2.down instead.

You're also using a negative distance in your 3rd argument, which doesn't make any sense.

avatar image daniel14657 homer_3 · Sep 27, 2020 at 08:24 PM 0
Share

Yeah, The negative distance & the direction was the problem, I have changed the code to this and everything is working fine now.

     Is_Grounded = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
     Debug.DrawLine(transform.position, transform.position + Vector3.down, Color.black, 1f);

Thank you very much for your kindness and help @homer_3 , Appreciate it!

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

134 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 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 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 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 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

Raycast distance affected by momentum of character 0 Answers

Reset raycast when an object exits 1 Answer

Fixed bullet spread 1 Answer

physics.raycast nullReferenceException 1 Answer

Move raycast.point to transform.position 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