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

相关推荐
dadaobusi2 小时前
学习:奈奎斯特
学习
WiChP4 小时前
【V0.1B16】从零开始的2D游戏引擎开发之路
开发语言·算法·游戏引擎
传奇开心果编程4 小时前
【Go入门练中学】第2课:条件与循环
后端·学习·golang
小小de风呀4 小时前
de风——【从零开始学C++】(二十)红黑树封装map和set
c++·学习
爱吃苹果的日记本5 小时前
数据结构第四课—线性表Linear List
数据结构·学习
昌原的儿子LEO5 小时前
ARM 汇编核心知识点总结
嵌入式硬件·学习
Ray Wang5 小时前
Context上下文工程
python·学习·ai编程
百里香酚兰5 小时前
【Unity学习笔记】如何把粉色Prefab还原
笔记·学习·unity
索端阳5 小时前
ARM Day02 学习总结:ARM汇编基础核心知识点梳理
汇编·学习·arm
专注仿真6 小时前
CMO模型文档-活动单元基类
c#·模型·cmo·活动单元基类