通过复制玩家位置跟随玩家的幽灵敌人 AI
Ghost enemy AI that follows the player by copying the players position
好的,计划很简单。
我的计划是制作一个简单的 AI,记录每个 player.positions,然后使用这些位置跟随玩家。所以 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;
}
如何改进它才能触摸到玩家?
在玩家接触区域的那一刻,方法OnTriggerExit2D()
被调用。然后,调用方法 PlayGhost()
并停止 Record()
。所以,幽灵无法在玩家出区后记录 player.positions。
您可以在方法 FixedUpdate()
中删除 else
来修复它。
private void FixedUpdate()
{
if (playing == true)
{
PlayGhost();
}
Record();
}
好的,计划很简单。 我的计划是制作一个简单的 AI,记录每个 player.positions,然后使用这些位置跟随玩家。所以 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;
}
如何改进它才能触摸到玩家?
在玩家接触区域的那一刻,方法OnTriggerExit2D()
被调用。然后,调用方法 PlayGhost()
并停止 Record()
。所以,幽灵无法在玩家出区后记录 player.positions。
您可以在方法 FixedUpdate()
中删除 else
来修复它。
private void FixedUpdate()
{
if (playing == true)
{
PlayGhost();
}
Record();
}