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

相关推荐
Amazing_Cacao6 小时前
深度观察 | 从“产区玄学”到“液态战场”:精品巧克力的终极试金石
学习
深蓝海拓7 小时前
S7-1500PLC学习笔记:MOVE_BLK、MOVE_BLK_VARIANT、BLKMOV的区别
笔记·学习·plc
darkhorsefly7 小时前
玩24算的益处
学习·游戏·24算
深蓝海拓9 小时前
S7-1500学习笔记:用户自定义数据类型(UDT)
笔记·学习·plc
罗罗攀9 小时前
PyTorch学习笔记|神经网络的损失函数
人工智能·pytorch·笔记·神经网络·学习
aP8PfmxS210 小时前
从零学习Kafka:数据存储
分布式·学习·kafka
AI成长日志11 小时前
【算法学习专栏】动态规划基础·中等两题精讲(198.打家劫舍、322.零钱兑换)
学习·算法·动态规划
arvin_xiaoting11 小时前
OpenClaw学习总结_III_自动化系统_1:Hooks详解
运维·学习·自动化
杜子不疼.12 小时前
Java 智能体学习避坑指南:3 个常见误区,新手千万别踩,高效少走弯路
java·开发语言·人工智能·学习
李苏苏2813 小时前
OpenClaw与CSDN Bot版本兼容配置指南
学习