当我的玩家进入 AI 的距离内时,AI 将其状态更改为 Flee 并调用此函数:
void Flee()
{
inTransit = false; //this makes sure he gets to his destination before rerouting
if(IsPlayerBlockingPath() && DistanceToTarget(player.position) < 10f)
{
PerpendicularMovement(HorizontalNormalToTarget(player.position));
}
else
{
MoveTowardsTarget(enemyBase.position);
}
}
我正在使用(我相信是)从 ai 到他的“enemyBase”的单位向量和从 ai 到玩家的单位向量的点积。如果它是一个介于 0.5 和 1 之间的值,那么这两个向量的方向相似,所以我们需要围绕玩家进行引导。然而,我的 AI 只在地图的某些部分绕着玩家而不是其他地方
float DistanceToTarget(Vector3 location)
{
return (transform.position - location).magnitude;
}
bool IsPlayerBlockingPath()
{
Vector3 AIToPlayer = HorizontalNormalToTarget(player.position);
Vector3 AIToEnemyBase = HorizontalNormalToTarget(enemyBase.position);
float dotP = Vector3.Dot(AIToPlayer, AIToEnemyBase);
if (dotP > blockTollerance) //blockTollerance is set to 0.1f
{
return true;
}
return false;
}
void PerpendicularMovement(Vector3 perpToThis)
{
Vector3 perpDirection = Quaternion.Euler(0, 90, 0) * perpToThis;
MoveTowardsTarget(perpDirection * perpMovementIncrement);
//I need him to move in this direction a bit further than he currently will
}