简体   繁体   English

我如何在代码中实现navmesh寻路到这个AI。 C#Unity

[英]How would I implement navmesh pathfinding into this AI following code. C# Unity

I have this code that makes the enemy follow my player(And attack etc) but im not sure how to add navmesh into this so it can navigate obstacles. 我有这个代码,让敌人跟随我的玩家(和攻击等),但我不知道如何添加导航到这,所以它可以导航障碍。 Currently, it goes forward and gets stuck on walls and obstacles. 目前,它向前发展并陷入墙壁和障碍。 I have never used navmesh before. 我之前从未使用过navmesh。

How would I implement navmesh pathfinding into this code. 我如何在此代码中实现navmesh寻路。

Thank you. 谢谢。

using UnityEngine;
using System.Collections;

public class wheatleyfollow : MonoBehaviour {

    public GameObject ThePlayer;
    public float TargetDistance;
    public float AllowedRange = 500;
    public GameObject TheEnemy;
    public float EnemySpeed;
    public int AttackTrigger;
    public RaycastHit Shot;

    void Update() {
        transform.LookAt (ThePlayer.transform);
        if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out Shot)) {
            TargetDistance = Shot.distance;
            if (TargetDistance < AllowedRange) {
                EnemySpeed = 0.05f;
                if (AttackTrigger == 0) {
                    transform.position = Vector3.MoveTowards (transform.position, ThePlayer.transform.position, EnemySpeed);
                }
            } else {
                EnemySpeed = 0;
            }
        }

        if (AttackTrigger == 1) {
            EnemySpeed = 0;
            TheEnemy.GetComponent<Animation> ().Play ("wheatleyattack");
        }
    }

    void OnTriggerEnter() {
        AttackTrigger = 1;
    }

    void OnTriggerExit() {
        AttackTrigger = 0;
    }

}

To start off, we will require a NavMeshAgent on the object that will hold this script and we will then save a reference to the agent. 首先,我们将需要一个NavMeshAgent该脚本的对象的NavMeshAgent ,然后我们将保存对代理的引用。 We will also need a NavMeshPath to store our path (This isn't an attachable component, we will create it within the code). 我们还需要一个NavMeshPath来存储我们的路径(这不是一个可NavMeshPath组件,我们将在代码中创建它)。

All we need to do is update the path using CalculatePath and SetPath . 我们需要做的就是使用CalculatePathSetPath更新路径。 You may need to fine tune the code some, but this is the very basics. 您可能需要对代码进行微调,但这是非常基础的。 You can use CalculatePath to generate a path then decide if you want to execute that path by using SetPath . 您可以使用CalculatePath生成路径,然后决定是否要使用SetPath执行该路径。

Note: We could use SetDestination , but if you have many AI units it can become slow if you need instant paths, which is why I normally use CalculatePath and SetPath . 注意:我们可以使用SetDestination ,但是如果你有许多AI单元,如果你需要即时路径,它会变慢,这就是我通常使用CalculatePathSetPath

Now all that is left is to make your navmesh Window -> Navigation . 现在剩下的就是创建navmesh Window -> Navigation In there you can finetune your agents and areas. 在那里你可以微调你的代理人和领域。 One required step is to bake your mesh in the Bake tab. 一个必需的步骤是在“ Bake选项卡中Bake网格。

Unity supports navmeshes on components for prefabs and other things, however, these components are not yet built into Unity, as you will need to download them into your project. Unity支持对预制件和其他东西的组件进行导航,但是,这些组件尚未内置到Unity中,因为您需要将它们下载到项目中。

As you can see all of your speed and movement has been removed since it is now controlled by your NavMeshAgent . 正如您所看到的那样,您的所有速度和移动都已被移除,因为它现在由您的NavMeshAgent控制。

using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class wheatleyfollow : MonoBehaviour {

  public GameObject ThePlayer;
  public float TargetDistance;
  public float AllowedRange = 500;
  public GameObject TheEnemy;
  public int AttackTrigger;
  public RaycastHit Shot;

  private NavMeshAgent agent;
  private NavMeshPath path;

  void Start() {
    path = new NavMeshPath();
    agent = GetComponent<NavMeshAgent>();
  }

  void Update() {
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot)) {
      TargetDistance = Shot.distance;
      if (TargetDistance < AllowedRange && AttackTrigger == 0) {
        agent.CalculatePath(ThePlayer.transform.position, path);
        agent.SetPath(path);
      }
    }
    if (AttackTrigger == 1) {
      TheEnemy.GetComponent<Animation>().Play("wheatleyattack");
    }
  }

  void OnTriggerEnter() {
    AttackTrigger = 1;
  }

  void OnTriggerExit() {
    AttackTrigger = 0;
  }

}

Side Note: You should remove any using 's that you are not using, as this can bloat your final build. 旁注:你应该删除任何using的是不使用的,因为这会增加你的最终版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM