- Home /
Question by
eXKR · Jan 06, 2010 at 10:38 PM ·
gameobjectraycast
How can a gameobject be deactivated if it is "seen" by the crosshair?
I know it should be done with raycasting method but I don't get the point on making it works... Any idea?
Thanks
Comment
Best Answer
Answer by Jaap Kreijkamp · Jan 06, 2010 at 10:53 PM
You need to do something like this:
- Add colliders to the gameobjects.
- Use Camera.main.ScreenPointToRay to create a ray from crosshair into world
- Use Physics.Raycast to find out what you're hitting
- Use SetActiveRecursivly on the gameobject the found collider is attached to to disable it
EDIT: okay, some code to help you:
function Update () {
if (Input.GetButtonDown("Fire1")) {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) {
// hit will now contain info about the object clicked upon
hit.transform.gameObject.SetActiveRecursively(false);
}
}
}
It helped and made my think in the correct way, thanks. Anyway I can't get it work: I'm lost at point 3... can you please make me figure out how it can be coded?
Thanks a lot
Added example code now so that should get you running again.