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

相关推荐
和侧3 小时前
离子注入后为什么必须配套退火工艺?
笔记·学习
凛铄linshuo4 小时前
自动化测试:pytest第一个实例学习
学习·pytest
FairGuard手游加固4 小时前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
我命由我123456 小时前
智能助手通用问答边界
人工智能·python·学习·ai·学习方法·ai编程·智能体
鬼手点金6 小时前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
几个几个n6 小时前
LaTeX 入门学习
学习
想七想八不如114088 小时前
元学习简单入门
人工智能·pytorch·python·深度学习·学习·机器学习
Awh-9 小时前
ARM 裸机开发 DAY11 学习笔记
arm开发·笔记·学习
2603_9695792811 小时前
U盘的系统备份怎样还原系统?详细步骤与注意事项
学习
下页、再停留11 小时前
【C#桌面客户端系列学习-11】将SQLite数据库打包到exe里
数据库·c#·visual studio