x


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

more ▼

asked Jan 06 '10 at 10:38 PM

eXKR gravatar image

eXKR
23 2 2 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to do something like this:

  1. Add colliders to the gameobjects.
  2. Use Camera.main.ScreenPointToRay to create a ray from crosshair into world
  3. Use Physics.Raycast to find out what you're hitting
  4. 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);
    	}
    }
}
more ▼

answered Jan 06 '10 at 10:53 PM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

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

Jan 08 '10 at 07:29 AM eXKR

Added example code now so that should get you running again.

Jan 09 '10 at 12:29 AM Jaap Kreijkamp
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2084
x1528

asked: Jan 06 '10 at 10:38 PM

Seen: 1282 times

Last Updated: Jan 06 '10 at 10:38 PM