- Home /
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.
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.
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
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
Follow this Question
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