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
1
Question by $$anonymous$$ · Nov 12, 2014 at 11:14 PM · physicsraycastdistancescreenpointtoraymaximum

Is there a limit on the distance of raycast?

I'm using this line of code:

 if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) 
     return;

And the purpose is to detect if the mouse cursor touched a mesh collider.

And it works just fine on the vast majority of objects. But when I tried it on a REALLY large sphere (the size of a small planet), the above line of code no longer works.

Is there a limit on the distance of either physics.raycast or Camera.main.ScreenPointToRay? And if so, is there a way to increase the distance?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 12, 2014 at 11:55 PM

No there isn't. If you don't provide a distance it will default to float.PositiveInfinity. So the ray will literally have a length of "infinity". Raycasting doesn't work by really shooting a ray which will "travel" though the space. The physics system direclty tests the intersection of the ray with any bounding box and finally with the actual colliders of the objects.

Keep in mind that if your ray starts inside a collider and goes "out" of the collider you won't hit the collider. You can only hit a collider on the outside.

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 $$anonymous$$ · Nov 13, 2014 at 01:17 AM 0
Share

Thanks, after testing it more, I think it has nothing to do with the distance from the object to me. It has to do with the size of the sphere itself. If I put a "sphere collider" on the sphere, the code works. But if I use mesh collider, the line above fails, if the mesh is so big. What's weird is, I know the mesh collider is there, because I can walk around on the sphere. But why does the raycast fail to trigger against the mesh collider, simply because the sphere is big...?

avatar image
0

Answer by toddisarockstar · Nov 16, 2014 at 04:24 AM

if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit, 100)) return;

your ray is limited to distance of 100 here in world space

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast not working on build 0 Answers

Is Raycast relatived to screen resolution? 1 Answer

Raycast stop at distance 3 Answers

Calculate where an object is going to land 0 Answers

How to get distances of GameObjects through SphereCast? 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