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.编写代码

相关推荐
烛阴7 分钟前
C#异常概念与try-catch入门
前端·c#
Radan小哥32 分钟前
Docker学习笔记—day011
笔记·学习·docker
星域智链38 分钟前
AI加持日常小节日:让每一份心意都精准升温✨
人工智能·科技·学习·生活·节日
jtymyxmz43 分钟前
《Unity Shader》12.2调整屏幕的亮度、饱和度和对比度
unity·游戏引擎
xiaoxiaoxiaolll1 小时前
《Light: Science & Applications》基于拓扑能量转移网络的UCNPs,实现65nm超分辨成像与10倍功耗降低
学习
可可苏饼干1 小时前
Ruo-Yi 前后端分离
运维·学习
Slaughter信仰2 小时前
图解大模型_生成式AI原理与实战学习笔记(前三章综合问答)
人工智能·笔记·学习
潮际好麦2 小时前
AI 工具推荐:AI绘图、AI助力学习
人工智能·学习
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [fs]binfmt_script
linux·笔记·学习
走在路上的菜鸟3 小时前
Android学Dart学习笔记第十二节 函数
android·笔记·学习·flutter