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 小时前
深入理解 Linux 物理内存管理
学习·操作系统
zx_zx_1234 小时前
哈希表的学习
学习·哈希算法·散列表
FAFU_kyp4 小时前
Kimi Coding Plan API 集成问题与解决方案
学习
weixin_537590455 小时前
《C程序设计语言》练习答案(练习1-13)
c语言·开发语言·c#
a17798877126 小时前
小程序上传图像失败
小程序·c#
JosieBook7 小时前
【C#】C# 所有关键字总结
开发语言·算法·c#
L-影7 小时前
下篇:它到底是怎么操作的——AI中半监督学习的类型与作用,以及为什么它成了行业的“最优解”
人工智能·学习·机器学习·ai·半监督学习
我是唐青枫7 小时前
C#.NET ConcurrentStack<T> 深入解析:无锁栈原理、LIFO 语义与使用边界
网络·c#·.net
xw-busy-code8 小时前
抽象语法书学习笔记
笔记·学习·ast·抽象语法树