- Home /
Problem is outdated
Make Raycast ignore anything that "Isn't" my player(Solved)
kind of a question in reverse but here goes.
I can get my enemy to track and fire at my player using a raycast and Tag combination.
if(Physics.Raycast(shootingRay, out hit, attackDistance))
{
if(hit.collider.tag == "Player")
{
InvaderFireWeapon();
}
}
But.. if I have an object between my enemy and the player it still fires as the raycast just goes through the obstruction?
Or to put it another way how do I make it so only a direct hit by the raycast will result in an attack
???
It looks like the obstacle doesn't have any collider attached. That's why raycast passes through it.
Answer by HappyLDE · Mar 28, 2015 at 06:04 PM
You need to yous the layermask:
public LayerMask layerMasks;
if (Physics.Raycast(shootingRay, out hit, attackDistance, layerMasks))
This won't help, if there's no collider on the obstacle...
The objects that are acting as obstacles do have colliders.
I tried adding a debug.log to the script and didn't get anything. So I added a debug.Drawray and started to check every object in the scene up close, rotation, size, transform position and DOH....
The raycast was missing the collider by 0.04 units. In perspective view everything was great, wasn't until I started using ortho view I noticed it.
Definitely one of those face meet desk moments....lol
Anyway, thanks guys
:/
gotta give a thumbs up just for the "face meets desk" comment, made me chuckle.