首页 > 解决方案 > 防止对角线运动

问题描述

我正在开发一款 3D 战术 RPG 游戏,我想知道如何在 NPC 移动时防止对角线移动?我希望他能检测到人类玩家在网格中的位置,然后去追他。

我可以采取他们的网格位置,但它没有用。NPC 进行对角线运动并开始在网格中“下沉”。我尝试过这样的事情,目标位置是向量 3,我在 Update() 中调用函数,这个脚本附加到 NPC:

public void MoveNPC()
    {
        HumanPlayer = GameObject.FindGameObjectWithTag("Player");
        ScriptPlayer PlayerPosition = HumanPlayer.GetComponent<ScriptPlayer>();
        NPC NPCPosition  = this.GetComponent<NPC>();


        if ((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) + PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
            (PlayerPosition.GridPosition.x -NPCPosition.GridPositonNPC.x) + (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax)
        {
            if((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
            (PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax){

                targetPosition = HumanPlayer.transform.position;
                targetPosition.y = 1.4f;
            }
        }

        if(this.transform.position != targetPosition)
        {
            this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
        }
    }

我该如何解决?

标签: unity3d

解决方案


推荐阅读