C# 我如何让统一的AI角色在玩家到达某个特定点的途中绕过他
当我的玩家到达AI附近时,AI会将其状态更改为逃跑,并调用此函数:C# 我如何让统一的AI角色在玩家到达某个特定点的途中绕过他,c#,unity3d,artificial-intelligence,vectormath,C#,Unity3d,Artificial Intelligence,Vectormath,当我的玩家到达AI附近时,AI会将其状态更改为逃跑,并调用此函数: void Flee() { inTransit = false; //this makes sure he gets to his destination before rerouting if(IsPlayerBlockingPath() && DistanceToTarget(player.position) < 10f) {
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);
}
}
逃跑
被叫的时间间隔是多少?如果距离为9.9f,AI将运行一点垂直移动
,然后立即向StarGet移动
,是否可能。更新函数正在调用flue。当我改变10f的值时,似乎没有什么真正的变化,就是你所指的距离,当AI决定垂直移动
或向Starget移动
时,你必须追踪的距离。检查您的标准是否过于宽松,因此大多数情况下,它会落在向StarGet移动的问题上。此外,还要检查除了您发布的代码片段之外,是否有任何代码会干扰AI的行为。调用fleed
的间隔是多长时间?如果距离为9.9f,AI将运行一点垂直移动
,然后立即向StarGet移动
,是否可能。更新函数正在调用flue。当我改变10f的值时,似乎没有什么真正的变化,就是你所指的距离,当AI决定垂直移动
或向Starget移动
时,你必须追踪的距离。检查您的标准是否过于宽松,因此大多数情况下,它会落在向StarGet移动的问题上。还要检查除了您发布的代码片段之外,是否有任何代码可能干扰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
}