Unity3d NavMeshAgent自动寻路组件

本文介绍了Unity3d中的NavMeshAgent组件用于自动寻路的方法,包括路点寻路、烘焙网格以及寻路参数设置。通过设置寻路区域、烘焙静态物体、调整寻路属性,可以实现游戏物体的智能导航。同时,文章提到了寻路区域的Cost值影响寻路路径的选择,以及如何通过代码控制寻路区域。

我们常见的有三种寻路方式

1.路点寻路

2.单元格寻路

3.网格寻路

简单介绍一下

1.路点寻路 如下图,物体从 point位置出发依次经过(point1 、point2、point3、point4、point5)进行移动

代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WayPointPath : MonoBehaviour {
    //所有路点的父物体
    private Transform point_parent;
    private int k=0;
	// Use this for initialization
	void Start () {
        //获取路点的父物体
        point_parent = GameObject.FindWithTag("Way").transform;

	}
	
	// Update is called once per frame
	void Update () {
        PointToPoint();
	}
    //物体按照路点移动
    void PointToPoint()
    {
        //判断物体到下一个路点的距离
        if (Vector3.Distance(transform.position, point_parent.GetChild(k).position) > 1f)
        {
            //用Vector3.Lerp移动
            transform.position = Vector3.Lerp(transform.position, point_parent.GetChild(k).position, 0.1f);
        }
        else
        {
            k=(k+1)%point_parent.childCount;
        }
    }
}

2.单元格寻路,

A*算法插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值