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 DJJD · Oct 27, 2017 at 01:29 AM · raycastjumpphysics2ddistancehit

Raycast always hit

Hi, I'm trying to make a double jump using Raycast. Sounds pretty simple, but for a reason my raycast hit all the time, and returns true even if there's nothing else in the scene and I'd like to know why.

Here is my code :

 public class CharacterControls : MonoBehaviour {
 
     private Rigidbody2D _rb;
     [SerializeField]
     private float _speed;
     [SerializeField]
     private float _jumpForce;
 
     private Collider2D _coll;
 
 void Update()
     {
 
         Debug.Log(Grounded());
         
         if (Grounded())
         {
             Debug.Log("Grouded");
         }
         else
         {
             Debug.Log("Oh Oh");
         }
 
 bool Grounded()
     {
         return (Physics2D.Raycast(transform.position,Vector2.down,_coll.bounds.extents.y + 0.1f));
     }
 }


What I'm trying to acheive is that the raycast goes down, with a distance just a bit lower than the collider. For a reason though, even if there is no ground, nothing else than the character in fact, Grounded() is always true. More than that, I can put any distance I want, even negative ones, and it will be true. What I'm doing wrong?

Thanks for the help!

Comment
Add comment · Show 2
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 Dragate · Oct 27, 2017 at 05:56 AM 0
Share

You can always debug storing the hit of the raycast in a raycasthit to deter$$anonymous$$e what it is hitting.

avatar image TanselAltinel · Oct 28, 2017 at 03:06 PM 0
Share

Is it possible that raycast hits the object itself? Can you try printing the name of the hit object?

1 Reply

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

Answer by Bunny83 · Oct 28, 2017 at 03:14 PM

The Physics2D system works different than the 3d Physics system. The Physics2D.Raycast method actually returns a RaycastHit2D struct. So checking if the struct is "null" / can be converted to bool makes no sense.

See the example on the Raycast documentation page i've linked. You have to check if the "collider" reference in the hit structure is null or not.

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 DJJD · Oct 28, 2017 at 05:11 PM 0
Share

I see, I was trying to use the same things as a 3D Raycast. But thanks! I checked if the collider was null or not, and applied a Layer $$anonymous$$ask so the Raycast does not hit the player itself.

Thanks man!

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

103 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

Related Questions

RaycastHit.distance 1 Answer

Hit distance Change light range 1 Answer

Some BoxCollider2D don't get hitted by no apparent reason 1 Answer

Raycast stop at distance 3 Answers

Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 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