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

相关推荐
莫非王土也非王臣8 小时前
深度学习之对比学习
人工智能·深度学习·学习
Wzx1980128 小时前
doker深学习
学习·docker
InterestOriented8 小时前
破解银发学习痛点 兴趣岛 “普惠 + 品质” 模式打造积极老龄化范本
大数据·人工智能·学习
HyperAI超神经9 小时前
IQuest-Coder-V1:基于代码流训练的编程逻辑增强模型;Human Face Emotions:基于多标注维度的人脸情绪识别数据集
人工智能·深度学习·学习·机器学习·ai编程
testpassportcn9 小时前
UiPath-ADPV1 認證介紹|Automation Developer Professional v1
网络·学习·改行学it
生擒小朵拉9 小时前
ROS1学习笔记(二)
笔记·学习
Gorgous—l12 小时前
数据结构算法学习:LeetCode热题100-动态规划篇(下)(单词拆分、最长递增子序列、乘积最大子数组、分割等和子集、最长有效括号)
数据结构·学习·算法
呆萌哈士奇12 小时前
告别 throw exception!为什么 Result<T> 才是业务逻辑的正确选择
c#·.net
窗边鸟13 小时前
小白日记之java方法(java复习)
java·学习
魔芋红茶14 小时前
Spring Security 学习笔记 4:用户/密码认证
笔记·学习·spring