- Home /
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
Answer by Bryan 4 · Apr 22, 2011 at 03:29 PM
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.
}
should'nt hit.collider.gameObject be part of the if statement? how would i use it in relation to my code ?
ive posted the code you gave me below and how im trying to use it...
Answer by Chris 35 · Apr 23, 2011 at 02:10 AM
@ 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?
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 :)
Answer by MC HALO · Apr 23, 2011 at 04:16 AM
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 :)
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"){
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
or my hotmail
i sent you an email on your gmail email containing the project with the script
i have fixed it :) i am going to update the answer if other people need help :)
Your answer
Follow this Question
Related Questions
Need help with building system 1 Answer
Creating a teleportation gun 1 Answer
How to detect raycast exiting 1 Answer
Ray Casting - Trigger function 1 Answer
Raycast and RaycastHit failure 2 Answers