- Home /
Something fishy with RaycastHit
I've been trying to detect whether enemies are in line of sight in my game, but for some reason, even though all the raycasting appears to be working properly, it doesn't seem to want to execute the commands when it verifies that it hit the target.
var hit : RaycastHit;
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (var enemy : GameObject in enemies)
{
var dummyVec : Vector3;
dummyVec = enemy.transform.position - transform.position;
Debug.DrawRay(transform.position, dummyVec, Color.red);
if (Physics.Raycast (transform.position,dummyVec, hit)){
print(hit.transform.tag);
if (hit.transform.tag == Enemy)
{
enemyLoS = true;
print("ouch");
}
}
}
I've also tried setting the if statement to "if (hit.transform = enemy)" as well as that is the name of the game object variable it is testing in that iteration of the for loop.
If anyone could shed any light on my conundrum I would really appreciate it. Thank you.
ah wow, i feel foolish now and can't believe I was hung up on that for so long. It works after I put quotations around it. Thank you.
it happens to the best of us. :)
mark as answered and have a nice day. :)
Answer by sparkzbarca · Nov 16, 2012 at 05:05 AM
tags are strings.
hit.transform.tag == "Enemy"
assuming they are tagged right. How did that even compile? How does unity know what Enemy is?
it should have thrown an error on that line.
Your answer
Follow this Question
Related Questions
raycast not detecting my mesh from blender 1 Answer
Help with Raycast C# 0 Answers
Using raycast and collider to increase int 1 Answer
NullReference when checking tag via Raycast. How to solve this? 1 Answer
Raycast exit point of collider 0 Answers