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 scottstephan · Oct 14, 2013 at 11:10 PM · raycastmousedistance

Physics.raycast distance not working?

I have a pretty simple script: The user clicks on the screen and I raycast from the point that they click at and report back. Its meant to mimic a touch-based interface.

My problem is that while it happily casts a ray, it seems to ignore my distance parameter. As a result, there are a whole mess of objects I literally can't hit, no matter what I change my distance to. Code is below- I feel like I'm missing something obvious.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit; Debug.Log("Firing Ray"); Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.yellow);
if (Physics.Raycast(ray, out hit,rayDistance,collectiblesLayer)){ //distance not working! Debug.Log ("Hit something: " + hit.transform.name); Debug.DrawLine (Camera.main.transform.position, hit.point, Color.red);
checkHit(hit.transform.gameObject); }
Comment
Add comment · Show 5
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 robertbu · Oct 14, 2013 at 11:17 PM 0
Share

The Raycast() looks fine. Are you sure it is the distance? Could you have messed up the layer mask? What happens if you leave off distance off (and therefore use infinity)?

avatar image scottstephan · Oct 14, 2013 at 11:21 PM 0
Share

If I leave off the distance like this: Physics.Raycast(ray, out hit)

Then everything is fine. Its definitely not the layer mask, because I can collect about 90% of the things in my scene. Its the 10% that are the farthest away that I can't hit, making me thing its the distance.

avatar image scottstephan · Oct 14, 2013 at 11:26 PM 0
Share

I have no idea what I did, but it seems to be working now. $$anonymous$$y suspicion is that maybe the controller was too close to the ground or something and it was clipping when I pointed in a certain direction. That makes no sense, but distance is suddenly working like a charm.

avatar image mattssonon · Oct 15, 2013 at 07:43 AM 0
Share

Please post an answer and accept it so the question appears answered. :)

avatar image Benproductions1 · Oct 15, 2013 at 07:52 AM 0
Share

Then you wouldn't $$anonymous$$d closing the question would you? :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Meatloaf4 · Feb 22, 2014 at 11:39 AM

Chances are you were having the same problem I was. The raycast is more than likely hitting an invisible collider that you have set up.

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 Meatloaf4 · Feb 22, 2014 at 08:53 AM 0
Share

Chances are you were having the same problem I was. The raycast is more than likely hitting an invisible collider that you have set up.

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

17 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

Related Questions

Distance from raycast. 1 Answer

Turn in the direction of movement 0 Answers

Mouse plane does not detect height 1 Answer

Is Raycast relatived to screen resolution? 1 Answer

RaycastHit.distance 1 Answer


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