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

相关推荐
爱上好庆祝2 小时前
学习js的第七天(wed APIs的开始)
前端·javascript·css·学习·html·css3
风兮雨露3 小时前
VMware虚拟机(安装/绿色版)
学习
CHANG_THE_WORLD4 小时前
C语言中的 %*s 和 %.*s 和C++的字符串格式化输出
c语言·c++·c#
zl_dfq5 小时前
python学习8 之 【集合、datetime模块、字典】
学习
kdxiaojie5 小时前
U-Boot分析【学习笔记】(3)
linux·笔记·学习
MediaTea5 小时前
Scikit-learn:从数据到结构——无监督学习的最小闭环
人工智能·学习·算法·机器学习·scikit-learn
@杰克成6 小时前
Java学习26
java·学习·idea
qeen876 小时前
【数据结构】二叉树相关经典函数C语言实现
c语言·数据结构·c++·笔记·学习·算法·二叉树
dingxingdi6 小时前
如何学习一个新的 Coding CLI 工具
学习
mxwin6 小时前
unity shader中 ddx ddy是什么
unity·游戏引擎·shader