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

相关推荐
菜的不敢吱声4 小时前
swift学习第4天
服务器·学习·swift
想进部的张同学8 小时前
hilinux-3599---设备学习---以及部署yolo
学习·yolo·海思
HyperAI超神经8 小时前
【vLLM 学习】Rlhf
人工智能·深度学习·学习·机器学习·vllm
李建军9 小时前
界止签章宗地号替换工具
c#
qq_425263329 小时前
.net开发框架和语言
c#
武藤一雄9 小时前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
flysh0510 小时前
C# 架构设计:接口 vs 抽象类的深度选型指南
开发语言·c#
一只一只10 小时前
Unity之协程
unity·游戏引擎·协程·coroutine·startcoroutine
做cv的小昊10 小时前
【TJU】信息检索与分析课程笔记和练习(7)数据库检索—Ei
数据库·笔记·学习·全文检索
AI360labs_atyun10 小时前
上海打出“开源”国际牌!2025重磅新政
人工智能·科技·学习·ai·开源