【问题标题】:Ghost enemy AI that follows the player by copying the players position通过复制玩家位置跟随玩家的幽灵敌人 AI
【发布时间】:2021-04-05 13:02:44
【问题描述】:

好的,计划很简单。 我的计划是制作一个简单的 AI,记录每个玩家的位置,然后使用这些位置跟随玩家。所以AI总是会落后玩家一些步骤。但是当玩家停止移动时,AI 会与玩家发生碰撞,然后玩家就会死亡。

所以我的问题是当玩家被AI追赶时,它总是在敌方AI能够接触玩家之前跑出位置......

我使用 Queue 来制作职位列表,这是我尝试使用 List 后有人推荐的。 Here's a video showing the problem

 public Transform player;
public Transform ghostAI;

public bool recording;
public bool playing;
Queue<Vector2> playerPositions;

public bool playerLeftRadius;




void Start()
{
    playerPositions = new Queue<Vector2>();

}

// Update is called once per frame
void Update()
{

    if (playerLeftRadius == true)
    {
        StartGhost();
    }

    Debug.Log(playerPositions.Count);


}

private void FixedUpdate()
{
    if (playing == true)
    {
        PlayGhost();
    }
    else
    {
        Record();
    }
}


void Record()
{
    recording = true;
    playerPositions.Enqueue(player.transform.position);
}

void PlayGhost()
{

    ghostAI.transform.position = playerPositions.Dequeue();       
    
    
}

public void StartGhost()
{
    playing = true;
}

public void StopGhost()
{
    playing = false;
}


private void OnTriggerExit2D(Collider2D other)
{
    Debug.Log("Player leaved the zone");
    playerLeftRadius = true;

}

如何改进它以使其能够接触到玩家?

【问题讨论】:

    标签: c# arrays unity3d queue position


    【解决方案1】:

    在玩家触摸区域的那一刻,方法OnTriggerExit2D() 被调用。然后,调用方法PlayGhost() 并停止Record()。因此,幽灵无法在玩家出区后记录 player.positions。 您可以在方法FixedUpdate() 中删除else 来修复它。

    private void FixedUpdate()
    {
        if (playing == true)
        {
            PlayGhost();
        }
        Record();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多