Hello I’m currently making AI for my game. I need their line of sight broken when a friendly unit moves in front of them. However each AI has the same layer on them on them to break other friendlies line of sight. This in term makes the AI break their line of sight from themselves. I’m very new to layers and linecasts and I’m unaware of how to approach this problem.
Does anyone have an idea how to prevent a linecast from seeing a layer attached to the gameobject or the gameobject’s child that it is being casted from?
This is my code I’m using.
public LayerMask FriendlyLayer;
private bool FriendlyBlocked;
if (Physics.Linecast (Target.transform.position, transform.position, out hit, FriendlyLayer))
{
FriendlyBlocked = true;
Debug.Log ("Hit");
}
else
{
FriendlyBlocked = false;
}
I just made it so the linecast now shoots from in front of the AI. However if anyone knows another way I’d be happy to hear it! Since I just want to learn as much as I can.
Mostly I just do the same thing and move the linecast so it starts in front of the source AI object.
You can also change the layer of your AI (in code) before the linecast and change it back afterwards, that way you can fire the linecast without worrying about it hitting the object it’s cast from.
e.g.
int DefaultLayer = LayerMask.NameToLayer( "Default" );
int AILayer = LayerMask.NameToLayer( "AI" );
currentObject.layer = DefaultLayer;
// Do linecast
currentObject.layer = AILayer;