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

相关推荐
Alphapeople1 分钟前
Isaac Sim学习
学习
蒸蒸yyyyzwd1 分钟前
高并发40问学习笔记
笔记·学习
天若有情6732 小时前
循环条件隐藏陷阱:我发现了「同循环双条件竞态问题」
c++·学习·算法·编程范式·while循环··竞态
咕白m6252 小时前
C# 高效复制 Word 文档内容
后端·c#
Amazing_Cacao2 小时前
褪去故事滤镜:重建精品可可的“结构语言”
笔记·学习
网络工程小王3 小时前
【大数据技术详解】——Sqoop技术(学习笔记)
大数据·学习·sqoop
暖阳之下3 小时前
学习周报三十六
学习
zlpzlpzyd3 小时前
groovy学习
java·jvm·学习
泯仲3 小时前
从零起步学习MySQL 第十二章:MySQL分页性能如何优化?
数据库·学习·mysql
我要成为嵌入式大佬3 小时前
嵌入式学习找工作第十七天--第二个项目(命令行日记本)
学习