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

相关推荐
程序员正茂21 小时前
Unity3d使用MQTT异步连接服务端
mqtt·unity·异步
mxwin1 天前
在unity shader中,通过pass产生阴影,通过主pass的光照 接收阴影!那么问题来了,是先产生阴影吗?还是先接收阴影,执行顺序是啥呢
数码相机·unity·游戏引擎·shader
小贺儿开发1 天前
《唐朝诡事录之长安》——盛世马球
人工智能·unity·ai·shader·绘画·影视·互动
百锦再1 天前
Auto.js变成基础知识学习
开发语言·javascript·学习·sqlite·kotlin·android studio·数据库开发
KuaCpp1 天前
C++新特性学习
c++·学习
Komorebi_99991 天前
大模型学习day5
学习·大模型
hixiong1231 天前
C# TensorRT部署RF-DETR目标检测&分割模型
人工智能·目标检测·计算机视觉·ai·c#
逍遥德1 天前
AI时代,计算机专业大学生学习指南
java·javascript·人工智能·学习·ai编程
网络与设备以及操作系统学习使用者1 天前
直连路由优先级最高
运维·网络·学习·华为·智能路由器
逆羽飘扬1 天前
【AI Infra面试】基础学习汇总篇
人工智能·学习