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

相关推荐
小小帅呀15 分钟前
学习 VLA 第 4 天:Transformer 原理与复现实战
深度学习·学习·transformer
消费知多少22 分钟前
解析勤策签约大西洋焊接费用核销实践案例
开发语言·c#
CSDN_RTKLIB24 分钟前
HashSet、Dictionary
c#
曹牧1 小时前
C#与Java后台交互
java·windows·microsoft·c#
问天_观心1 小时前
深入学习Transformer(一)
人工智能·python·深度学习·神经网络·学习·transformer
爱莉希雅&&&1 小时前
Git 版本控制完整学习笔记
笔记·git·学习
明王明王1 小时前
从零搭建一个单节点 K8S 可观测实验室(十三):总结——这个实验室如何服务工作、学习和开源项目
学习·kubernetes·开源
Romantic_love_1 小时前
理解USART真实收发过程
开发语言·stm32·单片机·学习
MartinYeung51 小时前
[论文学习]潜伏代理:训练能够经受安全训练的欺骗性大语言模型深度分析
学习·安全·语言模型
撩得Android一次心动1 小时前
Kotlin 语言【知识点整理3】
java·开发语言·笔记·学习·kotlin