Здравствуйте, мне нужна помощь в моем проекте, я хочу сделать игру наподобие "Dead Ahead Zombie Warfare "
, теперь я хочу реализовать алгоритм A * для поиска пути на каждом подразделении вигра, и я следую инструкциям на YouTube https://www.youtube.com/watch?v=dn1XRIaROM4
, когда я разверну одну единицу, чтобы следить за движущимся объектом, нет проблем.Но ... когда я хочу развернуть 2 юнита на одном и том же сценарии и следовать за движущимся объектом, как будто он застрял друг на друге
, пожалуйста, помогите мне, спасибо.
это скрипт для следованияпуть, когда путь найден:
using UnityEngine;
using System.Collections;использование UnityEngine.UI;
открытый класс Единица измерения: MonoBehaviour {
public Transform target;
float speed = 5;
Vector3[] path;
int targetIndex;
public Image healtbar;
int darah=100;
void Start() {
}
public void kenaserang(int jum)
{
darah -= jum;
healtbar.fillAmount = darah/100f;
}
void carimusuhterdekat(){
float jarakterpendek = Mathf.Infinity;
musuh musuhterdekat = null;
musuh[] semuamusuh = GameObject.FindObjectsOfType<musuh>();
foreach (var musuhsekarang in semuamusuh)
{
float jarakmusuh=(musuhsekarang.transform.position-this.transform.position).sqrMagnitude;
if (jarakmusuh<jarakterpendek)
{
jarakterpendek = jarakmusuh;
musuhterdekat = musuhsekarang;
target = musuhterdekat.transform;
}
}
}
private void FixedUpdate()
{
carimusuhterdekat();
PathRequestManager.RequestPath(transform.position, target.position, OnPathFound);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "musuh")
{
Destroy(other.gameObject);
}
}
public void OnPathFound(Vector3[] newPath, bool pathSuccessful)
{
if (pathSuccessful)
{
path = newPath;
targetIndex = 0;
StopCoroutine("FollowPath");
StartCoroutine("FollowPath");
}
}
IEnumerator FollowPath() {
Vector3 currentWaypoint = path[0];
while (true) {
if (transform.position == currentWaypoint) {
targetIndex ++;
if (targetIndex >= path.Length) {
yield break;
}
currentWaypoint = path[targetIndex];
}
transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);
yield return null;
}
}
public void OnDrawGizmos() {
if (path != null) {
for (int i = targetIndex; i < path.Length; i ++) {
Gizmos.color = Color.black;
Gizmos.DrawCube(path[i], Vector3.one);
if (i == targetIndex) {
Gizmos.DrawLine(transform.position, path[i]);
}
else {
Gizmos.DrawLine(path[i-1],path[i]);
}
}
}
}
}
это когда я развертываю только 1 единицу
это когда я развертываю 2 или более юнитов, они не очень хорошо следуют за юнитами