x


Detect Object that are hit by a ray

I have this script which casts a ray when the mouse button is clicked

var Bullet1_direction = Vector3(15,0,45); var Bullet2_direction = Vector3(0 ,0,1); var Bullet3_direction = Vector3(-9 ,0,45); var length = 4;

function Update () {

var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction); Bullet1_diagonal.Normalize();

var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction); Bullet2_diagonal.Normalize();

var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction); Bullet3_diagonal.Normalize();

if(Input.GetMouseButtonDown(0)){

 Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);
 Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);
 Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);

 if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
   Debug.Log ("Bullet 1 hit red!");
 }
 if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) {
   Debug.Log ("Bullet 2 hit red!");
 }
 if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) {
   Debug.Log ("Bullet 3 hit red!");
 }

}

}

what i want to do it make it so what ever object the ray hits it returns the name of the object and destroys that object. for example im using it for a top down ish shooter game so the object it hits will be an enemy however there will be more then one of the same enemy in the scene at once so can you make it that what ever object the ray hits to destroy that singular ibect not every one of thoes object?

help would be awesome

more ▼

asked Apr 22, 2011 at 01:46 PM

Chris 35 gravatar image

Chris 35
86 38 28 36

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

3 answers: sort voted first

you can use an out RaycastHit as one of the parameters for Physics.Raycast (look at its overloaded options) Then

if(Physics.Raycast())
{
 hit.collider.gameObject <---- the game object that was hit.
}
more ▼

answered Apr 22, 2011 at 03:29 PM

Bryan 4 gravatar image

Bryan 4
469 25 18 30

should'nt hit.collider.gameObject be part of the if statement? how would i use it in relation to my code ?

Apr 23, 2011 at 01:59 AM Chris 35

ive posted the code you gave me below and how im trying to use it...

Apr 23, 2011 at 02:11 AM Chris 35
(comments are locked)
10|3000 characters needed characters left

@ Bryan this is how im interpretation the code you gave me

if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) { if(Physics.Raycast(hit.collider.gameObject)){ if(gameObject.tag == ("Enemy")){ Debug.Log ("kill");

} } }

except i get a null reference error Help?

more ▼

answered Apr 23, 2011 at 02:10 AM

Chris 35 gravatar image

Chris 35
86 38 28 36

that will give you errors because you can not do :

 if(Physics.Raycast(hit.collider.gameObject)){

it should be like :

 if(hit.collider.gameObject.tag == "Enemy1"){
 Destroy(hit.collider.gameObject);

have a look at my answer below :)

Apr 23, 2011 at 04:21 AM MC HALO
(comments are locked)
10|3000 characters needed characters left

Well from what i read i hope is correct you have multiple enemies right and when the ray collides with the bad guy you want to destroy the game object. Now the easiest way that i can think of is creating variables at the top which carry gameObject in this case your enemies. every time your ray collides variable set gameObject with the enemy in it the ray will destroy the gameObject with the right tag. here is what i would do:

var Bullet1_direction = Vector3(15,0,45);

 var Bullet2_direction = Vector3(0 ,0,1);

  var Bullet3_direction = Vector3(-9 ,0,45);

   var length = 4;



     var Enemy1 : GameObject;

       var Enemy2 : GameObject;

       var Enemy3 : GameObject;





    function Update () {



           var Hit : RaycastHit;



    var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);

    Bullet1_diagonal.Normalize();



   var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);

    Bullet2_diagonal.Normalize();



   var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);

     Bullet3_diagonal.Normalize();





    if(Input.GetMouseButtonDown(0)){





    Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);

         Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);

             Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);



  if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

       Debug.Log ("Bullet 1 hit red!");

         if(Hit.collider.gameObject.tag == "Enemy1"){

             Destroy(Hit.collider.gameOject); 



  }



 }



if (Physics.Raycast(transform.position, Bullet2_diagonal,Hit,length)) {

   Debug.Log ("Bullet 2 hit red!");

      if(Hit.collider.gameObject.tag == "Enemy2"){

         Destroy(Hit.collider.gameOject); 



   }



      }

 if (Physics.Raycast(transform.position, Bullet3_diagonal,Hit,length)) {

   Debug.Log ("Bullet 3 hit red!");

      if(Hit.collider.gameObject.tag == "Enemy3"){

       Destroy(Hit.collider.gameOject); 



       }

    }

 }



}

Now the way that this works is really simple if the ray hits the following enemy stored in the gameObject it will destroy it. don't forget to assign the game object in your inspector mode. If this does not work please let me know thanks :)

Problem Update :

i have solved your problem :) basically the problem was in the ray itself, this is because if you remember on the top of update i created a variable called var Hit: RayCastHit

this became a major part in your ray because every time it would shoot the ray out i was told to destroy an object that was stored in the hit variable. So all i need to do was add the following to the following line:

if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

   Debug.Log ("Bullet 1 hit red!");

     if(Hit.collider.gameObject.tag == "Enemy1"){

         Destroy(Hit.collider.gameObject); 

i have pasted the code that had the changes you will now notice that after diagonal i have placed "HIT" Now we have told the ray once you shoot out your diagonal bullet look for the stored object in the hit. And then at the bottom we have just stored the hit to enemy 1 and then destroy the object. now the second thing you may find different is the Destroy call instead of using the 3 var gameobjects at the top i just told it to destroy the object that is collided with the hit in this case an Enemy that has the tag "Enemy1"

hope it helps :)

more ▼

answered Apr 23, 2011 at 04:16 AM

MC HALO gravatar image

MC HALO
968 258 211 225

I keep getting a null reference error Object reference not set to an instance of an object and the error keep pointing me to theses three lines of code if(Hit.collider.gameObject.tag == "Enemy1"){ if(Hit.collider.gameObject.tag == "Enemy2"){ if(Hit.collider.gameObject.tag == "Enemy3"){

Apr 23, 2011 at 01:14 PM Chris 35

have your enemies been tagged with the names Enemy1 , 2,3 and then tried if it still did not work please send me your file on my google account and i will have a look for you

my e-mail address

Hummad.Nazir@gmail.com

or my hotmail

gto_oni-eyes@hotmail.com

Apr 23, 2011 at 04:26 PM MC HALO

hope to hear from you

Apr 23, 2011 at 04:26 PM MC HALO

i sent you an email on your gmail email containing the project with the script

Apr 24, 2011 at 06:10 AM Chris 35

i have fixed it :) i am going to update the answer if other people need help :)

Apr 24, 2011 at 04:06 PM MC HALO
(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:

x6480
x3762
x2996
x301
x289

asked: Apr 22, 2011 at 01:46 PM

Seen: 6111 times

Last Updated: Apr 22, 2011 at 01:51 PM