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

相关推荐
王维志6 小时前
使用C#控制台批量删除 Unity目录里的 .meta文件
unity·c#·.net
de之梦-御风6 小时前
【C#.Net】C#开发的未来前景
开发语言·c#·.net
非凡ghost7 小时前
Wireshark中文版(网络抓包工具)
网络·windows·学习·测试工具·wireshark·软件需求
de之梦-御风7 小时前
【C#.Net】C#在工业领域的具体应用场景
开发语言·c#·.net
wdfk_prog8 小时前
[Linux]学习笔记系列 -- [driver]base
linux·笔记·学习
am心9 小时前
学习笔记-套餐接口
笔记·学习
wuk9989 小时前
基于C#与三菱PLC通过TCPIP实现MC协议通信示例
java·网络·c#
科技林总9 小时前
【系统分析师】3.6 操作系统
学习
avi91119 小时前
Unity 天命6源码- 商业游戏说明分析
开发语言·unity·c#·游戏开发·游戏源码
世洋Blog10 小时前
Unity脚本生命周期(全)
unity·游戏引擎