Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一向量2.在一定的时间内移动_C#_Unity3d_Artificial Intelligence - Fatal编程技术网

C# 统一向量2.在一定的时间内移动

C# 统一向量2.在一定的时间内移动,c#,unity3d,artificial-intelligence,C#,Unity3d,Artificial Intelligence,我在为我的NPC设置AI时遇到问题。我想让它在我的地图上的任意点周围走动,并在玩家靠近时逃离他。当我追逐我的npc时,逃跑是很好的,但当我停下来时,它们会朝着玩家来回弹跳,而不仅仅是设定另一个目的地。。。 这是密码。我将runToRandomLocation放在Update方法中 void runAway() { if (!isDead) { transform.position = Vector2.MoveTowards(transform.position,

我在为我的NPC设置AI时遇到问题。我想让它在我的地图上的任意点周围走动,并在玩家靠近时逃离他。当我追逐我的npc时,逃跑是很好的,但当我停下来时,它们会朝着玩家来回弹跳,而不仅仅是设定另一个目的地。。。 这是密码。我将runToRandomLocation放在Update方法中

void runAway()
{
    if (!isDead)
    {
        transform.position = Vector2.MoveTowards(transform.position, player.transform.position, -movementSpeed * 1.5f * Time.deltaTime);            
    }
}

void runToRandomLocation()
{
    if (!isDead) {

        if (Vector2.Distance(transform.position, player.transform.position) > 3)    // if player is not near
        {
            if (Vector2.Distance(transform.position, randomDestination) <= 2)   // when NPC is close to destination he sets another
            {
                randomDestination = new Vector2(Random.Range(-11, 11), Random.Range(-5, 5));
            }
            else
            {
                transform.position = Vector2.MoveTowards(transform.position, randomDestination, movementSpeed * Time.deltaTime);   // NPC is just walking from point to point
            }
        }
        else
        {
            runAway();  // if player is near
        }
    }
}

只有在到达上一个目的地时,才能生成新的随机目的地。这里似乎发生的是,在核动力源逃逸足够远之后,它将继续移动到逃跑前的最后一个随机目的地,因此它将返回。可能在玩家的方向。因此,在一帧之后,它再次靠近玩家,然后再次逃跑。然后在循环中再次返回到旧目的地,以此类推。 你需要的只是在逃跑结束后重新生成随机目的地。为此,你需要一些状态机,正如@Retired Ninja所指出的,但它实际上是一个非常原始的状态机。例如,类似这样的方法应该有效:

private bool onTheRun = false;

void regenDestination() {
    randomDestination = new Vector2(Random.Range(-11, 11), Random.Range(-5, 5));
}

void runAway() {
    if (!isDead) {
        transform.position = Vector2.MoveTowards(transform.position, player.transform.position, -movementSpeed * 1.5f * Time.deltaTime);
        onTheRun = true;
    }
}

void runToRandomLocation() {
    if (!isDead) {

        if (Vector2.Distance(transform.position, player.transform.position) > 3)    // if player is not near
        {
            if (onTheRun)
            {
                regenDestination();
                onTheRun = false;
            }
            if (Vector2.Distance(transform.position, randomDestination) <= 2)   // when NPC is close to destination he sets another
            {
                regenDestination();
            } else {
                transform.position = Vector2.MoveTowards(transform.position, randomDestination, movementSpeed * Time.deltaTime);   // NPC is just walking from point to point
            }
        } else {
            runAway();  // if player is near
        }
    }
}

您确实需要一个状态机来实现这一点,但是如果您的代码实际上没有对该目标执行任何操作,那么首先设置一个随机目标。它不会移动任何东西。