【发布时间】:2021-10-10 09:33:29
【问题描述】:
我正在开发像 Hitman Go 这样的游戏,当敌人通过岩石或声音等发出警报时,我需要找到离我的目标最近的航点(特定航点)。
我为敌人设置了一些点,我的敌人在航路点(8 -> 6 -> 1-> 2-> 3-> 4-> 5 )之间巡逻,然后尊重他的路径。 所以,当我在 18 号航点扔石头时,我需要将敌人移动到这个航点,但要以最近的方式。当他得到警报时,成像的敌人可以是任何这个航点(8,6,1,2,3,4,5 点)。
注1:两点之间的距离相同。
注意 2:我的游戏是 3d 而非 2d。
我使用这段代码一步一步地移动我的敌人(转弯基地)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaypointController : MonoBehaviour
{
public List<Transform> waypoints = new List<Transform>();
private Transform targetWaypoint;
private int targetWaypointIndex = 0;
private float minDistance = 0.1f;
private int lastWaypointIndex;
public bool reversePath;
// what easetype to use for iTweening
public iTween.EaseType easeType = iTween.EaseType.easeInOutExpo;
// how fast we move
public float moveSpeed = 1.5f;
// time to rotate to face destination
public float rotateTime = 0.5f;
// delay to use before any call to iTween
public float iTweenDelay = 0f;
// Use this for initialization
void Start()
{
lastWaypointIndex = waypoints.Count - 1;
targetWaypoint = waypoints[targetWaypointIndex];
}
public void EnemyTurn()
{
float distance = Vector3.Distance(transform.position, targetWaypoint.position);
CheckDistanceToWaypoint(distance);
// move toward the destinationPos using the easeType and moveSpeed variables
iTween.MoveTo(gameObject, iTween.Hash(
"x", targetWaypoint.position.x,
"y", targetWaypoint.position.y,
"z", targetWaypoint.position.z,
"delay", iTweenDelay,
"easetype", easeType,
"speed", moveSpeed
));
}
void CheckDistanceToWaypoint(float currentDistance)
{
if (currentDistance <= minDistance)
{
targetWaypointIndex++;
UpdateTargetWaypoint();
}
}
void UpdateTargetWaypoint()
{
if (targetWaypointIndex > lastWaypointIndex)
{
if (reversePath)
{
waypoints.Reverse();
}
targetWaypointIndex = 1;
}
targetWaypoint = waypoints[targetWaypointIndex];
}
}
【问题讨论】:
-
Unity 3D已经有一个内置的寻路系统,A*也不少
-
@Micky,谢谢,但有没有办法找到没有 A* 或统一导航网格的壁橱方式?
标签: unity3d path-finding waypoint