- Home /
Raycasting question
I have a character that I want to track his eyes to where the mouse is pointing. To do this I set up a box collider and positioned it in the camera's view (see screenshot). I wrote a script for the collider to raycast from the mouse and point the eyes at the result. This works great.
However, the collider is preventing anything else from being raycast onto because it's blocking the whole scene. I have the character set up to do an animation if clicked, but it doesn't work while I have the collider box there.
Is there a way for me to only enable the collider box right before I raycast, then turn it off? I have tried setting hideFlags, but that didn't work. I also tried moving the collider box out of view, then temporarily back into view for the raycast, but that didn't work either.
Any ideas? Seems like this should be pretty straightforward.
Answer by DaveA · Mar 09, 2011 at 07:31 PM
Do you need to use a collider? I would suggest converting the mouse position to a world position, then have the eyes LookAt that point. If you need the collider, maybe putting it in a different layer would help.
I would prefer to do what you described, but I don't know how to convert mouse coords to world coords without using raycasting.
O$$anonymous$$G, that's exactly what I needed. I'm so new to Unity that I'm still learning the native functions. Thanks!
No problem. I find the documentation is quite excellent, many times the exact bit of code I need is already there as an example. And it's google-able!
Answer by Tuck · Mar 09, 2011 at 07:32 PM
There is a layer called ignoreRaycasts or something you could use. At the start of a frame, set the collider's layer to a raycast-able layer, track the eyes to the mouse, then set the collider layer back to ignoreRaycasts.
Your answer
Follow this Question
Related Questions
Internal collisions 1 Answer
3D text with box collider Android 1 Answer
Paddle which control by Cpu for Air hockey game 0 Answers
Is it possible to skew a box collider? 1 Answer
how to resize box collider? 4 Answers