Thursday, July 26, 2012

How to tell if the play has selected a 3d object

function Update () {


    if (Input.GetButtonDown ("Fire1"))
     {
     
         var hits : RaycastHit[]; // what we hit
        
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // creating a ray from the mouse pos
         hits = Physics.RaycastAll (ray.origin, ray.direction, 1000); // creating a ray that will check hits along ray
         Debug.DrawLine(ray.origin,ray.direction,Color.blue); // this is a debug option that will allow us to see the ray in debug mode.



// the second variable is how long it is...
 //I set it ridiculously high to make sure I hit everything
.  
        if (Physics.Raycast (ray, 1000)) {
        for (var i = 0;i < hits.Length; i++)  // for each hit in hits.
         {
        var hit : RaycastHit = hits[i]; // check this particular ray.
        var tag =  hit.collider.tag; // get the tag for the hit.
        if (tag == "Panda") { // if the hit object is "panda"
            print("got Panda"); // then we print panda.. .you can also add code inside this.
      
                                   }
          }
           
        }
     }

No comments:

Post a Comment