【问题标题】:Stuck trying to make the AI in Turn-Based Strategy Game试图在回合制战略游戏中制作 AI
【发布时间】:2013-12-12 18:46:34
【问题描述】:

我已经可以通过寻路制作移动网格并为玩家避开障碍物。现在我想根据移动网格和可用的动作点让 AI 自己移动(就像玩家一样),但我不知道该怎么做。现在,我只能让角色移动到该位置(但它不跟随寻路,这个角色假设是AI)。我一直试图解决这个问题,但不能。你们能帮帮我吗?谢谢。

这是我目前得到的代码(它使假定为 AI 的角色移动到该位置,但它不跟随寻路):

using UnityEngine;
using System.Collections;

public class AIPlayer : Player
{
    void Awake()
    {
        moveDestination = transform.position;
    }

    // Use this for initialization
    void Start()
    {
        ColorChanger();
    }

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

    }


    public override void TurnUpdate()
    {
        if (GameManager.instance.currentPlayerIndex == 5)
        {
            if (Vector3.Distance(moveDestination, transform.position) > 0.1f)
            {
                transform.position += (moveDestination - transform.position).normalized * moveSpeed * Time.deltaTime;

                if (Vector3.Distance(moveDestination, transform.position) <= 0.1f)
                {
                    transform.position = moveDestination;
                    actionPoints--;
                }
            }

            else
            {
                moveDestination = new Vector3(2 - Mathf.Floor(GameManager.instance.mapSize / 2), 1.5f, -2 + Mathf.Floor(GameManager.instance.mapSize / 2));
                GameManager.instance.NextTurn();
            }
        }

        base.TurnUpdate();
    }

    public override void TurnOnGUI()
    {

    }

    public override void ColorChanger()
    {
        base.ColorChanger();
    }
}

这是游戏的链接视频:

http://www.youtube.com/watch?v=eo7DzbBPQBs&feature=youtu.be

【问题讨论】:

    标签: c# unity3d artificial-intelligence


    【解决方案1】:

    那里没有用于寻路的代码。这只是一个过于复杂的 lerp。同样,直接移动到该位置会导致图形避障问题严重。

    最好的办法是实现基于网格的导航网格并使用 A* 搜索之类的东西来查找路径。移动将被限制在相邻的方格内,因此不会发生图形避障。

    看看this tutorial。除了动画玩家运动和目标位置的游戏逻辑之外,它将为您提供所需的一切。

    【讨论】:

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