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

相关推荐
寒月小酒4 小时前
第三章 索引构建(2-all -in-rag学习)
学习
天国梦4 小时前
中学生居家英语听力训练深度解析:从核心痛点到AI赋能的全链路方案
人工智能·学习
胡渠洋9 小时前
postman学习
学习·测试工具·postman
asdzx679 小时前
使用 Python 精准操控 Word 字体:获取与替换方案
python·c#·word
Amazing_Cacao10 小时前
CFCA精品可可产区风土解析(美洲):无情打破风味堆叠假象,建立时间轴上的层次动态阅读系统
学习
六点_dn10 小时前
Linux学习笔记-shell运算符
linux·笔记·学习
其实防守也摸鱼11 小时前
渗透--损坏的对象级别鉴权漏洞(Broken Object Level Authorization, BOLA)
大数据·网络·数据库·学习·tcp/ip·安全·安全威胁分析
念雨思11 小时前
信用卡权益对比:基于HarmonyOS + ArkTS的AI智能金融助手开发实践
人工智能·学习·华为·金融·harmonyos·鸿蒙
铅笔侠_小龙虾11 小时前
Rust 学习(2)-变量、常量与 shadowing
学习·算法·rust
it小生010111 小时前
从底层思维到高效行动,顶尖人才进阶核心修炼指南
学习·学习方法