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 分钟前
27| 魔法封印
数据结构·c++·笔记·学习·算法
2501_918126916 分钟前
stm32能做次声波检测器吗?
c语言·stm32·单片机·嵌入式硬件·学习
好好学习天天向上~~7 分钟前
13_Linux_学习总结_进程终止
linux·学习
今儿敲了吗8 分钟前
30| 木材加工
数据结构·c++·笔记·学习·算法
锅包一切15 分钟前
PART7 队列
c++·学习·算法·leetcode·力扣·刷题·队列
xhyu6119 分钟前
【学习笔记】推荐系统 (3.召回:矩阵补充、线上服务、双塔模型)
笔记·学习·矩阵
陆嵩22 分钟前
从一个小例子学习方程组求解超节点(supernodal)算法
学习·算法·amd·重排·超节点·supernadal·消去树
cgsthtm28 分钟前
使用c#oracle19c和sqlsugar实现erp单据新增时单据编号自增
c#·sqlsugar·oracle19c·单据编号自增·erp单据编号
游乐码28 分钟前
c#里氏替换
开发语言·c#
未来之窗软件服务29 分钟前
AI人工智能(十二)C# 运行sensevoice onnx—东方仙盟练气期
开发语言·人工智能·c#·仙盟创梦ide·东方仙盟