After following along with the Pluggable AI with Scriptable Objects tutorial here : link text
I’ve been working on implementing it into my project and so far it’s been working much better than all the other methods I’ve tried I feel much more comfortable with this approach. However when using a SphereCast to simulate the AI’s field of view I can’t seem to get the AI to notice the player when they are standing on a short wall in front of the AI.
Currently my AI will begin in a patrolling state, following a path of waypoints around two obstacles that for simplicity sake I have named walls. I have set a 3rd wall at the edge of one side of the path, so as the AI approaches the corner the 3rd wall will be directly within the AI field of view. I position the player on this wall, which is only 4 units tall, to test if the AI notices the player. I’ve tried giving the AI an incredibly unfair, large view radius (up to 10.0f), used for the radius of the SphereCast of course, and yet still the AI doesn’t notice the player.
I know that this is because the SphereCast is colliding with the wall before the player, but how can I work around that when I can’t ignore the wall layer altogether as that will mean the AI could see the player through walls?
Here’s a screenshot of what the scene looks like:
The red spheres represent the waypoints and the green sphere represents the AI’s view radius, currently the radius is set to 7.0f, so it reaches just 2 units taller than the walls.
Here’s the code the AI uses to detect the player:
[CreateAssetMenu(menuName = "PluggableAI/Decisions/Look")]
public class LookDecision : Decision
{
public override bool Decide(StateController controller)
{
bool targetVisible = Look(controller);
return targetVisible;
}
private bool Look(StateController controller)
{
RaycastHit hit;
Debug.DrawRay(controller.eyes.position, controller.eyes.forward.normalized * controller.enemyStats.lookRange, Color.green);
if (Physics.SphereCast(controller.eyes.position, controller.enemyStats.lookRadius, controller.eyes.forward, out hit, controller.enemyStats.lookRange,
controller.enemyStats.ignoreLayer)&& hit.collider.CompareTag("Player"))
{
controller.target = hit.transform;
return true;
}
else
{
return false;
}
}
}