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

相关推荐
科技前言17 分钟前
程序bug修复的全过程
学习·编程
蓝裕安39 分钟前
DesignMode__unity__抽象工厂模式在unity中的应用、用单例模式进行资源加载
unity·游戏引擎
__water2 小时前
『功能项目』宠物的攻击巨型化【80】
c#·unity引擎·宠物随机巨型化攻击
AutoAutoJack2 小时前
C#的属性(Property)应用说明(一)
开发语言·数据结构·算法·c#
Deeper5202 小时前
吊打 NVM 的 4 款 Node.js 版本工具!
学习·npm·node.js·生活·前端学习
问道飞鱼2 小时前
每日学习一个数据结构-Trie树(字典树)
数据结构·学习
chao_6666662 小时前
【深度】为GPT-5而生的「草莓」模型!从快思考—慢思考到Self-play RL的强化学习框架
人工智能·深度学习·学习·机器学习
丹丹的笑意2 小时前
学习记录:js算法(四十七):相同的树
javascript·学习·算法
Ace'3 小时前
学习笔记&&每日一题
笔记·学习·算法
mez_Blog3 小时前
React学习笔记(3.0)
前端·笔记·学习·react.js·前端框架