- Home /
Raycast ignoring tags problem
Hi everyone, I don't know whats wrong here. I using raycasts for my AI to avoid obstacles and I don't want them to avoid some obstacles such as the player or obstacles that can be moved. But they avoid everything. So its suppose to avoid everything except player and movable objects, but instead it just avoids everything.
Here's one snippet of the code. I have multiple raycasts but they're all the same only with a different origin and ray distance.
if(Physics.Raycast(transform.position, transform.forward, hit, rayDistance1))
{
if(hit.collider.gameObject.tag != "Player" || hit.collider.gameObject.tag != "Movable" || hit.collider.gameObject.tag != "WeaponPickup");
{
Debug.DrawLine(transform.position, hit.point, Color.red);
lookDirection += hit.normal * repelForce;
}
}
else
{
Debug.DrawRay(transform.position, transform.forward * rayDistance1, Color.yellow);
}
I don't know whats wrong. I tried both hit.collider.tag != "Player" and hit.transform.tag != "Player" and the hit.collider.gameObject.tag != "Player", but its the same result for anything.
Thank you.
Use && ins$$anonymous$$d of || inside your if. Right now you're saying "If its not the player, OR its not WeaponPickup"... One of them will always be true. If it's the player, then it's not the WeaponPickup, and since you used OR, it will always enter the if.
Here's with && statements. And I tried using hit.collider.tag ins$$anonymous$$d of hit.collider.gameObject.tag, but it makes no difference.
if(Physics.Raycast(transform.position, transform.forward, hit, rayDistance1))
{
if(hit.collider.tag != "Player" && hit.collider.tag != "$$anonymous$$ovable" && hit.collider.tag != "WeaponPickup");
{
Debug.DrawLine(transform.position, hit.point, Color.red);
lookDirection += hit.normal * 0;
}
}
else
{
Debug.DrawRay(transform.position, transform.forward * rayDistance1, Color.yellow);
}
EDIT: I also commented out other rays since there's five total, to avoid chance of interference, but its the same.
Glad it helped. Accept Owen's answer please so other users know this is solved.
Answer by Owen-Reynolds · Nov 02, 2013 at 11:44 PM
Your tag IF is messed up -- it's always true. No matter what tag you hit, it's always not the other two tags, so the answer is always true. Whenever you combine not-equals in an IF, should be using AND (&&) instead of OR. A good Intro to programming textbook might cover this in chapter 4.
Then a bunch of other things could be wrong. Check tags are set correctly. Print more stuff -- the line after the raycast add print commands like Debug.Log("hit: "+ hit.transform);
.
I see the logic and I tried but its still debugging as red and avoiding.