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 thenachotech1113 · Dec 05, 2013 at 01:00 AM · physicsraycast

i want a raycast to only collide with an object, and viceversa

so i have a sphere where raycast from the camera will collide, I only want that raycast to collide with that sphere, not with anything that could get in the way (say a bullet or something). also I want that sphere to not let any other raycast.

p.s. please note that I don't ask you to write code for me, simply to explain me how to make it work, that way I will understand it if any tweeks are needed in the future.

thank you for all your 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
2
Best Answer

Answer by OP_toss · Dec 05, 2013 at 01:03 AM

Instead of raycasting by using Physics.Raycast, you instead call raycast on the collider itself. This only performs the raycast against that specific collider.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.Raycast.html

BTW Thanks for not asking for code! I didn't feel like your question implied that, so no worries.

Comment
Add comment · Show 2 · 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 thenachotech1113 · Dec 05, 2013 at 02:15 AM 0
Share

thanks, this really helps, i asked for code this time because last time I was expecting a link or something but someone showed up with a huge code. really appreciate it and it did the trick, but it was not very helpful since I did not understand it

avatar image OP_toss · Dec 06, 2013 at 10:51 PM 0
Share

Oh sorry if I was unclear.. Basically if you call Physics.Raycast, it will perform a raycast on every collider in the scene. This is less efficient and not what you want. Since you only want to check a ray against a single particular collider, then you want to use the Collider.Raycast ins$$anonymous$$d. This checks a ray only against a single collider. This can be much more efficient, as there are some very easy calculations that can rule out a collision entirely before actually computing a raycast. Like if your object is behind the ray origin and the rays pointing away, it will obviously never collide. This also ignores other objects on the way to your object, which allows you to be more particular in your raycast tests. If you're needing to perform this type of per-collider raycast on more than 4 or 5 colliders an update, I would just use a global raycast ins$$anonymous$$d.

Hope that helped clear it up for you!

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

Picking up few different objects 0 Answers

SphereCast Arguments 1 Answer

Debug.DrawLine problem 1 Answer

Having a RaycastHit event create a component? 1 Answer

Problem With RayCast 3 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