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 小时前
【Linux加餐】mmap文件映射
linux·运维·服务器·c语言·c++·学习
叫我黎大侠7 小时前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
叶子野格7 小时前
《C语言学习:编程例题》8
c语言·开发语言·c++·学习·算法·visual studio
唐青枫7 小时前
C#.NET ValueTaskSource 深入解析:零分配异步、ManualResetValueTaskSourceCore 与使用边界
c#·.net
公子小六7 小时前
基于.NET的Windows窗体编程之WinForms事件简介
windows·microsoft·c#·.net
迷你可可小生7 小时前
面经学习(二)
学习·算法
夜瞬8 小时前
NLP学习笔记05:命名实体识别(NER)入门——从规则方法到 BERT
笔记·学习·自然语言处理
军训猫猫头8 小时前
7.带输入参数的线程启动 C# + WPF 完整示例
开发语言·前端·c#·.net·wpf
遗憾随她而去.8 小时前
react学习(一)
javascript·学习·react.js