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

相关推荐
Sc Turing11 分钟前
【每日AI学习0607】
人工智能·学习
winlife_13 分钟前
全程用 AI 做一款商业级手游 · EP9 收尾与复盘:做到了哪,没做到哪,边界在哪
java·开发语言·人工智能·unity·ai编程·游戏开发·mcp
晓131337 分钟前
【Cocos Creator 2.x】篇——第五章 游戏常用关键技术
前端·javascript·vue.js·游戏引擎
caimouse1 小时前
2D 与 3D 跨平台游戏引擎
游戏引擎
学机械的鱼鱼1 小时前
一文读懂轮足翼复合机器人:结构特点与仿真学习路线规划
学习·机器人
123的故事1 小时前
工具分享(2)-NSmartProxy内网穿透工具。
c#·.net·nsmartproxy
知识分享小能手2 小时前
Hadoop学习教程,从入门到精通, 部署Hadoop 3.x — 知识点详解(2)
大数据·hadoop·学习
EQ-雪梨蛋花汤2 小时前
【Unity笔记】Unity URP 透明玻璃出现白色光斑?Directional Light 镜面高光问题分析与解决
3d·unity·数字孪生
星恒随风2 小时前
C++ 类和对象入门(三):拷贝构造、赋值运算符重载和深浅拷贝
开发语言·c++·笔记·学习