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

相关推荐
冒泡P37 分钟前
【Unity】TextMeshPro富文本中使用精灵图集
ui·unity·c#·游戏引擎
世洋Blog1 小时前
开发思想-(数据驱动+组合模式)VS 继承
unity·组合模式·数据驱动
世洋Blog1 小时前
开发思想-组合模式和接口多态的一点思考
c#·组合模式
会编程的李较瘦2 小时前
【Spark学习】数据清洗
学习·ajax·spark
RisunJan2 小时前
【行测】实词辨析
学习
B0URNE2 小时前
【Unity基础详解】(9)Unity核心:UI系统
ui·unity·游戏引擎
m0_626535203 小时前
双线性插值学习
学习
YJlio3 小时前
进程和诊断工具学习笔记(8.24):Handle——谁占着不放?句柄泄漏排查、强制解锁与检索技巧
服务器·笔记·学习
梵克之泪3 小时前
【号码分离】从Excel表格、文本、word文档混乱文字中提取分离11位手机号出来,基于WPF的实现方案
开发语言·ui·c#
charlie1145141913 小时前
面向C++程序员的JavaScript 语法实战学习4
开发语言·前端·javascript·学习·函数