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 分钟前
MySQL学习(7)——存储过程
学习
似水明俊德28 分钟前
02-C#
开发语言·c#
似水明俊德1 小时前
01-C#.Net-泛型-面试题
java·开发语言·面试·c#·.net
野奔在山外的猫1 小时前
【解决】IndexOutOfRangeException: renderPassIndex
unity
小酒丸子1 小时前
AD学习笔记之异形焊盘
笔记·学习
renhongxia11 小时前
如何对海洋系统进行知识图谱构建?
人工智能·学习·语言模型·自然语言处理·自动化·知识图谱
似水明俊德2 小时前
07-C#
开发语言·c#
知识分享小能手2 小时前
Redis入门学习教程,从入门到精通,Redis 数据操作:知识点详解与代码实战(2)
数据库·redis·学习
似水明俊德2 小时前
12-C#
开发语言·数据库·oracle·c#
red_redemption3 小时前
自由学习记录(138)
学习