- Home /
How can i chose the previous target
I want to chose a target by Ray, and change it's color to red. And when Im not pointing on the same object ( target) or choseing another target, i want it to turn back to it's original color. How can i make that?
Answer by kolban · May 06, 2012 at 04:40 PM
In the Update() function you will likely cast the Ray and determine what GameObject the ray struck. If it struck a game object, record the game object that was struck in a variable as well as recording its current material color. Now change the color of the GameObject to be what you want it to be.
In the Update() function, when the Ray strikes an object (see above), also compare to whether it was the SAME object as previously detected (currentGO == savedGO) or ... if the ray didn't strike any object, then using the previous savedGO (if one exists), set its color to be the previously saved color.
This alternative is better when you may select any object in scene, because the script is attached to a single object (usually the main camera). Be aware that it works only for objects having a collider or CharacterController - GUITexture and GUIText aren't sensible to raycasts.
Answer by Berenger · May 06, 2012 at 04:41 PM
The easy is to use unity built-in rays on colliders. In your case, OnMouseEnter would change the color, OnMouseExit would reset it.
I want to chose target when it's in my crosshair, and not when its under my mouse
This alternative is good when only some objects can be selected - you must place the selection script in all selectable objects, because On$$anonymous$$ouse events are sent only to the script attached to the selected object.
On$$anonymous$$ouseEnter/Exit are called when the mouse enters/exits the object region, while On$$anonymous$$ouseDown is called when the user clicks the object.
Be aware that only GUIText, GUITexture and objects that have colliders or CharacterControllers originate On$$anonymous$$ouseEvents.