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

相关推荐
Century_Dragon4 小时前
让实训“活”起来——信息化综合实训考核平台助力汽车专业教学
学习
nashane5 小时前
HarmonyOS 6学习:PanGesture手势驱动月亮半圆轨迹“滚动”术
学习·harmonyos 5
tedcloud1236 小时前
ppt-master部署教程:快速搭建智能演示文稿系统
服务器·人工智能·系统架构·游戏引擎·powerpoint
叶~小兮6 小时前
K8S进阶核心综合学习笔记(持久化存储+特殊容器+调度管理)
笔记·学习·kubernetes
li星野6 小时前
位运算 & 数学 & 高频进阶九题通关(Python + C++)
c++·python·学习·算法
祀爱6 小时前
Asp.net core+ Layui 项目中编辑按钮传递数据的方法
前端·c#·asp.net·layui
y = xⁿ8 小时前
Java并发八股学习日记
java·开发语言·学习
@codercjw8 小时前
工程图制图经验
学习
星幻元宇VR8 小时前
VR文旅大空间|沉浸式体验重塑文旅新场景
科技·学习·安全·vr·虚拟现实
Cat_Rocky9 小时前
K8S-Helm简单学习分享
学习·容器·kubernetes