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

相关推荐
feiduoge7 小时前
教程 44 - 相机系统
windows·游戏引擎·图形渲染
Larry_Yanan7 小时前
Qt多进程(一)进程间通信概括
开发语言·c++·qt·学习
csdn_aspnet7 小时前
浅谈 C# 与 Data URI
c#
做cv的小昊8 小时前
【TJU】信息检索与分析课程笔记和练习(1)认识文献
经验分享·笔记·学习·搜索引擎·全文检索
烛阴9 小时前
C# 正则表达式:量词与锚点——从“.*”到精确匹配
前端·正则表达式·c#
再睡一夏就好9 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
蒙奇D索大9 小时前
【11408学习记录】考研英语长难句拆解三步法:三步拆解2020年真题,攻克阅读难点
笔记·学习·考研·改行学it
好奇龙猫9 小时前
【日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(32):本階段が終わります】
学习
悠闲漫步者9 小时前
第2章 MCS-51单片机的串口和最小系统(学习笔记)
笔记·学习·51单片机
shenghaide_jiahu9 小时前
数学分析简明教程——6.5
学习