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

(207评价)
优惠后 ¥1899.00优惠前 ¥2499.00 双十一全年最大力度限时优惠
1对1 答疑即时回复7天无理由退款30天价保提问必答

服务保障

1对1 答疑

提供专属的一对一在线答疑,快速答疑解惑。

即时回复

及时响应学习咨询,帮助解决问题。

7天无理由退款

购买后7天内可申请无理由退款。

30天价保

购买后30天内如遇降价,可申请价保。

提问必答

对于提问做到有问必答,帮您扫清学习过程中的种种障碍

Stealth里面机器人无法准确寻路问题
chengjun发起了问答2018-05-11
2
回复
780
浏览

EnemyAnimation脚本代码与老师写法一样:

void Update () {
        if (navAgent.desiredVelocity==Vector3.zero)
        {
            anim.SetFloat("Speed", 0);
            anim.SetFloat("AnglarSpeed", 0);

        }else
        {
            float angle=Vector3.Angle(transform.forward, navAgent.desiredVelocity);
           float newAngle =angle* Mathf.Deg2Rad;
            
            if (angle>90)
            {
                anim.SetFloat("Speed", 0);
            }else
            {
                Vector3 project=Vector3.Project(navAgent.desiredVelocity,transform.forward);
                anim.SetFloat("Speed", project.magnitude);
            }
            Vector3 crossRes = Vector3.Cross(transform.forward, navAgent.desiredVelocity);
            if (crossRes.y < 0)
            {
                newAngle = -newAngle;
            }
            anim.SetFloat("AnglarSpeed", newAngle);
         
        }

        anim.SetBool("PlayerInside", sight.playerInsight);
        
    }

 

EnemyMoveAI脚本也一致:(除了弃用的API,都没改过)

 private void Update()
    {
        
            if (sight.playerInsight&&health.hp>0)
            {
                //射击
                Shooting();
            }
            else if (sight.alertPosition != Vector3.zero && health.hp > 0)
            {
                Chasing();
            }
            else
            {
                Patrolling();
            }
        
         
        
    }
    //射击方法
    private void Shooting()
    {
        navAgent.isStopped = true;

    }
    //巡逻方法
    private void Patrolling()
    {
        navAgent.isStopped = false;
        navAgent.speed = 3;
        navAgent.updatePosition = false;
        navAgent.updateRotation = false;
        if (navAgent.remainingDistance<0.5f)
        {
            navAgent.isStopped = true;//让导航暂停
            patrolTimer += Time.deltaTime;
            if (patrolTimer> patrolTime)
            {
                patrolTimer = 0;
                index++;
                index %= 4;//保证可以循环
                navAgent.destination = wayPoints[index].position;
                navAgent.updatePosition = false;
                navAgent.updateRotation = false;
                navAgent.isStopped = false;

            }

        }
    }
    //追捕
    private void Chasing()
    {
        navAgent.isStopped = false;
        navAgent.speed = 3;
        navAgent.updatePosition = false;
        navAgent.updateRotation = false;
        navAgent.destination = sight.alertPosition;
        if (navAgent.remainingDistance<2f)
        {
            chaseTimer += Time.deltaTime;
            if (chaseTimer>chaseTime)
            {
                sight.alertPosition = Vector3.zero;
                GameController.Instance.lastPlayerPosition = Vector3.zero;
                GameController.Instance.alermOn = false; 
                
            }
        }
    }

 

 

但是运行时候机器人无法寻路到指定位置,总是差很多

要不就是撞墙

机器人走到这里就停下了。。。然后一直撞墙,乱转

 

但是如果把EnemyMoveAI脚本中的所有

navAgent.updatePosition = false;
navAgent.updateRotation = false;语句注释掉之后,机器人是可以自动准确寻路的

可能问题出在了EnemyAnimation脚本里面。但是对了代码,貌似没问题啊。

不知道问题出在了哪里,求老师解答一下

所有回复
  • siki • 2018-05-12

    应该是转向速度过慢导致的,可以调整下转向速度和行走速度

    转向速度过慢的话,就跟不上所寻的路径

    还有-5条回复,点击查看
    你还没有登录,请先登录或注册!
  • loveyou2000228 • 2020-12-16

    老师有steralth新版的教程吗老版教程做啦几遍的机器人老是不能追捕只能待机动画和举起抢还没伤害

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