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

相关推荐
小贺儿开发11 分钟前
Unity3D 文物互动大屏
3d·unity·实时互动·udp·socket·网络通信
pyniu20 分钟前
Elasticsearch学习
后端·学习·elasticsearch·搜索引擎
liliangcsdn23 分钟前
IMPALA强化学习算法的学习和解读
学习·算法
蒸蒸yyyyzwd25 分钟前
os八股学习笔记
笔记·学习
野犬寒鸦35 分钟前
Java8 ConcurrentHashMap 深度解析(底层数据结构详解及方法执行流程)
java·开发语言·数据库·后端·学习·算法·哈希算法
郝学胜-神的一滴40 分钟前
在Vibe Coding时代,学习设计模式与软件架构
人工智能·学习·设计模式·架构·软件工程
科技林总1 小时前
【系统分析师】9.5 容灾与业务持续
学习
宇木灵1 小时前
C语言基础-六、指针
c语言·开发语言·学习·算法
春和景明3601 小时前
费曼学习法8
学习
人工智能研究所1 小时前
从 0 开始学习人工智能——什么是推理模型?
人工智能·深度学习·学习·机器学习·语言模型·自然语言处理