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

相关推荐
未来之窗软件服务24 分钟前
幽冥大陆(四十一)美萍V10酒店门锁SDK C#语言仙盟插件——东方仙盟筑基期
开发语言·c#·仙盟创梦ide·东方仙盟·东方仙盟sdk·酒店智能门锁·东方仙盟 vos 智能浏览器
疋瓞1 小时前
C++_win_QT6学习《3》_结合qt项目开发学习git仓库相关知识
c++·qt·学习
qq_205279051 小时前
Unity log工具 Unity Logviewer插件
unity·游戏引擎
Clarence Liu2 小时前
redis学习 (1) 基础入门
数据库·redis·学习
qq_571099352 小时前
学习周报二十五
学习
崇山峻岭之间2 小时前
C++ Prime Plus 学习笔记027
c++·笔记·学习
IMPYLH3 小时前
Lua 的 tonumber 函数
开发语言·笔记·后端·junit·游戏引擎·lua
Xudde.3 小时前
BabyPass靶机渗透
笔记·学习·安全·web安全
kblj55553 小时前
学习Linux——学习工具——DNS--BIND工具
linux·运维·学习
立志成为大牛的小牛3 小时前
数据结构——五十四、处理冲突的方法——开放定址法(王道408)
数据结构·学习·程序人生·考研·算法