How can I make a ray change the color of the object it hits first or use a boxcollider trigger instead?
No matter what i do with the function "OnTriggerEnter" or anything similar i get non satisfying results.
I would like it if when I point at an object in a (first person shooter) and it is in range then it turns green other wise red. I would like to manipulate the object if it is in range.
Any advice would be great, Thanks a ton!!
Okay, this is a fairly complex question but it piqued my interest :-)
You need to do this using Physics.Raycast to cast a ray from the centre of the screen (assuming that this is a standard first person shooter with a fixed crosshair in the centre of the screen).
You then need to make sure any targets have colliders, because the raycast will only detect objects with colliders. You'll also want to make sure all your targets have a certain tag (such as "Enemy") so that you don't go round turning your scenery green or red!
Because only one object at a time will be coloured, we can use a single variable ("originalColor") to store the original colour of the object, so that we can re-set it when it leaves the target area or when another object is detected.
Below, I have split up the functionality into a few different functions.
And here's the code:
answered Feb 16 '10 at 03:51 PM