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

相关推荐
不灭锦鲤20 小时前
网络安全学习第59天
学习·安全·web安全
weixin_5206498720 小时前
C#进阶-特性全知识点总结
开发语言·c#
楼田莉子20 小时前
同步/异步日志系统:日志落地模块\日志器模块\异步日志模块
linux·服务器·c++·学习·设计模式
fengyehongWorld21 小时前
C# 创建vba用的类库
c#
旖-旎21 小时前
递归(汉诺塔问题)(1)
c++·学习·算法·leetcode·深度优先·递归
澄澈青空~21 小时前
有一个叫R2C,也有一个叫G2C
java·数据库·人工智能·c#
SUNNY_SHUN21 小时前
清华团队提出TFA-Net,用模板特征聚合破解工业异常检测中的“捷径学习“难题
人工智能·学习·视觉检测·github
SuperHeroWu721 小时前
【鸿蒙基础入门】概念理解和学习方法论说明
前端·学习·华为·开源·harmonyos·鸿蒙·移动端
speop21 小时前
TASK05 | Reasoning Kindom拟合的陷阱 —— 统计相关性不是推理
学习
深念Y1 天前
感知机 ≈ 可学习的逻辑门?聊聊激活函数与二元分类的本质
人工智能·学习·分类·感知机·激活函数·逻辑门·二元分类