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

相关推荐
MartinYeung5几秒前
[论文学习]激活差异揭示后门:SAE架构对比研究
人工智能·学习·架构
小雪崩3 分钟前
嵌入式学习 day35:进程间通信-消息队列、共享空间及有名信号量
linux·c语言·学习
zhchyun200820 分钟前
# 【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(06)
ui·unity·游戏引擎
淡海水1 小时前
05-03-栈队列-PriorityQueue-TElement-TPriority-NET6优先队列语义与四叉堆实现
服务器·前端·c#·priorityqueue·clr·telement
一条破秋裤1 小时前
STM32 学习笔记:STM32 简介与学习路线
笔记·stm32·学习
念何架构之路1 小时前
Gin辅助模块与学习路径
学习·gin
具身AGI2 小时前
基座模型架构之争,物理AI 人类学习路线 的双脑答案
人工智能·学习·架构
浔溺2 小时前
al+大数据每日学习笔记27
笔记·学习
吃好睡好便好2 小时前
判断函数的使用
学习·算法·matlab·生活·判断函数
阳光宅男@李光熠3 小时前
【电子通识】排阻和普通电阻有什么区别?
笔记·学习