Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
秘密行动 敌人ai自动寻路
衡异发起了问答2018-11-22
3
回复
2098
浏览

敌人ai自动寻路时在导航点前停住,导致后面的导航都失败,unity版本是2017.1.1p4,运行网盘里的项目代码敌人的自动寻路也是有问题的

EnemyAnimation

using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class EnemyAnimation : MonoBehaviour
{

    public float speedDampTime = 0.3f;
    public float anglarSpeedDampTime = 0.3f;

    private NavMeshAgent navAgent;

    private Animator anim;


    void Awake()
    {
        navAgent = this.GetComponent<NavMeshAgent>();
        anim = this.GetComponent<Animator>();

    }

    // Use this for initialization
    void Start()
    {

    }

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

        if (navAgent.desiredVelocity == Vector3.zero)
        {
            anim.SetFloat("Speed", 0, speedDampTime, Time.deltaTime);
            anim.SetFloat("AnglarSpeed", 0, anglarSpeedDampTime, Time.deltaTime);

        }
        else
        {

            float angle = Vector3.Angle(transform.forward, navAgent.desiredVelocity);
            float angleRad = 0f;
            if (angle > 90)
            {
                anim.SetFloat("Speed", 0, speedDampTime, Time.deltaTime);
            }
            else
            {
                Vector3 projection = Vector3.Project(navAgent.desiredVelocity, transform.forward);
                anim.SetFloat("Speed", projection.magnitude, speedDampTime, Time.deltaTime);
            }
            angleRad = angle * Mathf.Deg2Rad;

            Vector3 crossRes = Vector3.Cross(transform.forward, navAgent.desiredVelocity);
            if (crossRes.y < 0)
            {
                angleRad = -angleRad;
            }

            anim.SetFloat("AnglarSpeed", angleRad, anglarSpeedDampTime, Time.deltaTime);

        }

       

    }
}

EnemyMoveAI

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyMoveAI : MonoBehaviour {

    public Transform[] wayPoints;
    public float patrolTime = 3f;
    private float patrolTimer = 0;
    private int index = 0;
    private NavMeshAgent navAgent;
     void Awake()
    {
        navAgent = this.GetComponent<NavMeshAgent>();
        navAgent.destination = wayPoints[index].position;
        navAgent.updatePosition = false;
        navAgent.updateRotation = false;

    }
   
     void Update()
    {
        patrolling();
    }
    private void patrolling()
    {
        if (!navAgent.pathPending && navAgent.remainingDistance < 0.5f)
        {
            
            patrolTimer += Time.deltaTime;
            if (patrolTimer>patrolTime)
            {
                index++;
                index %= 4;
              
                navAgent.destination = wayPoints[index].position;
                navAgent.updatePosition = false;
                navAgent.updateRotation = false;
                patrolTimer =0;
            }
        }
    }
}

运行截图

所有回复
  • 老师_Trigger 2018-11-23

    同学你好,在使用导航系统在控制移动的时候,因为视频是4.x版本,所以api变动。新版本用navAgent.nextPosition = transform.position;可以同步位置, 放在update里,如果出现移动的时候被墙体卡到可以看一下是动画问题还是旋转速度问题,再做进一步修改。

     

    还有-3条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!