unity自动寻路

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class Nav : MonoBehaviour

{

    public Transform player;

    private NavMeshAgent navMeshAgent;

    private Animator animator;

    public float attackTime = 0;

    // Start is called before the first frame update

    void Start()

    {

        navMeshAgent = this.GetComponent();

        animator = this.GetComponentInChildren();

    }

    // Update is called once per frame

    void Update()

    {

       

    }

    void FixedUpdate() {

        if (attackTime > 0) {

            attackTime -= Time.deltaTime;

        }

        // 计算距离

        float dis = (this.transform.position - player.position).magnitude;

        if (dis > 2) {

            navMeshAgent.isStopped = false;

            navMeshAgent.SetDestination(player.position);

            return;

        }

        Debug.Log(dis);

        navMeshAgent.isStopped = true;

        if (attackTime <=0) {

            // attack animator

            animator.SetTrigger("ATTACK");

            attackTime = 1; // 间隔:1s 一次

           

        }

    }

}

你可能感兴趣的:(unity,unity)