Unity 新导航寻路演示(2)

对于静态场景来说,只需要3步

1.为场景Ground添加网格表面组件并烘焙

2.为player添加导航代理

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMove : MonoBehaviour
{
    private NavMeshAgent agent;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
            { 
            Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit,1000))
                {
                agent.SetDestination(hit.point);
            }
        }
        Vector3 v =agent.velocity;
        if(v.magnitude >0.1)
            {
            animator.SetBool("isRunning",true);
        }
        if(v.magnitude < 0.1)
            {
            animator.SetBool("isRunning", false);
        }
    }
}

3.编写代码

相关推荐
宁清明2 小时前
【小宁的学习日记2 C语言】C语言判断
c语言·学习·算法
又是忙碌的一天5 小时前
算法学习 13
数据结构·学习·算法
一只一只8 小时前
Unity 3D笔记(进阶部分)——《B站阿发你好》
笔记·3d·unity·游戏引擎
微露清风8 小时前
系统性学习C++-第八讲-vector类
java·c++·学习
月临水9 小时前
Git 学习笔记
笔记·git·学习·1024程序员节
奋斗的牛马9 小时前
FPGA—ZYNQ学习Debug(三)
学习·fpga开发
qq_401700419 小时前
matlab学习
学习·算法·matlab
hd51cc9 小时前
C++ 类的学习(四) 继承
开发语言·c++·学习
行思理11 小时前
Docker 应该如何学习 分四个阶段
学习·docker·容器
大山同学11 小时前
Blender学习-基础移动
数码相机·学习·blender