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

相关推荐
老唐77715 分钟前
常见经典十大大机器学习算法分类与总结
人工智能·深度学习·神经网络·学习·算法·机器学习·ai
烟雨孤舟34 分钟前
python 基础学习文档
学习
ECT-OS-JiuHuaShan1 小时前
渡劫代谢,好事多磨
数据库·人工智能·科技·学习·算法·生活
xiaoshuaishuai82 小时前
C# modbustcp的ack包通信延迟原因
网络·tcp/ip·c#
2301_780943842 小时前
第三阶段:Gem5-GPU集成学习
学习
~kiss~2 小时前
quantizers 学习
学习
暖阳之下2 小时前
学习周报四十二
学习
wubba lubba dub dub7502 小时前
第四十四周 学习周报
学习
hixiong1233 小时前
使用C#自制一个截屏工具
c#
黑眼圈子3 小时前
动态规划问题专项练习(未编辑完成...
学习·算法·动态规划