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

相关推荐
Harm灬小海1 小时前
【云计算学习之路】学习Centos7系统-Linux下用户及组管理
linux·运维·服务器·学习·云计算
herinspace1 小时前
管家婆辉煌主机登录提示“连接失败,请确认输入正确的服务器名”
运维·服务器·学习·零售·管家婆软件·财务软件
佚泽2 小时前
C# webApi学习笔记
笔记·学习·c#
魔法阵维护师2 小时前
从零开发游戏需要学习的c#模块,第二十四章(瓦片地图 —— 让世界有墙)
学习·游戏·c#
吴可可1232 小时前
C#中括号报错“应输入标识符”原因解析
c#
文阿花3 小时前
Pc端大屏地图实现方案分析
学习
我的xiaodoujiao3 小时前
API 接口自动化测试详细图文教程学习系列21--结合Pytest框架使用2--断言和插件
python·学习·测试工具·pytest
炽烈小老头3 小时前
【每天学习一点算法 2026/05/22】课程表 II
学习·算法