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

相关推荐
科技林总19 小时前
2.2 图论应用
学习
陌上明苏20 小时前
.NET学习-依赖注入、配置系统、日志系统
学习
知识分享小能手20 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的Shell编程详细知识点(含案例代码)(17)
linux·学习·ubuntu
大山老树20 小时前
行动教练学习笔记
笔记·学习
副露のmagic1 天前
更弱智的算法学习 day16
数据结构·学习·算法
@zulnger1 天前
python 学习笔记(文件读写)
笔记·python·学习
微露清风1 天前
系统性学习C++-第十六讲-AVL树实现
java·c++·学习
YJlio1 天前
Kali Linux 外置无线网卡接入与识别排障(VMware 环境|合规学习版)
linux·网络·学习
韩明君1 天前
debian13学习笔记
服务器·笔记·学习
m5655bj1 天前
使用 C# 实现 Excel 工作表拆分
windows·c#·excel·visual studio