我正在尝试为同伴创建一个AI,让它们跟随玩家并定位到尚未被其他同伴占据的最接近的位置
我在同伴上有一个脚本:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ReachPosition : MonoBehaviour{ public GameObject[] firstLine = new GameObject[2]; Transform target; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { //计算到每个位置的距离 float dist = Vector3.Distance(transform.position, firstLine[0].transform.position); float dist1 = Vector3.Distance(transform.position, firstLine[1].transform.position); if (dist < dist1 && firstLine[0].GetComponent<Position>().isEmpty) { target = firstLine[0].transform; Debug.Log(0); } else if (dist > dist1 && firstLine[1].GetComponent<Position>().isEmpty) { target = firstLine[1].transform; Debug.Log(1); } //到达目标 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 1 * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, target.position, .01f); }
}
在位置上有一个触发器:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Position : MonoBehaviour { public bool isEmpty; // Start is called before the first frame update void Start() { isEmpty = true; } private void OnTriggerEnter(Collider other) { isEmpty = false; } private void OnTriggerExit(Collider other) { isEmpty = true; }
}
实际上它们确实到达了最近的位置,但它们并不关心该位置是否已被占据。(我还应该尝试使代码对“firstLine”游戏对象数组正常工作,现在这个数组的存在没有意义)对不起我的英语不好…
—————–编辑—————我改变了选择要到达的位置的方式,在编队脚本上工作:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Formation : MonoBehaviour{public List<GameObject> firstLine = new List<GameObject>();public List<GameObject> Companions = new List<GameObject>();// Start is called before the first frame updatevoid Start(){ }// Update is called once per framevoid Update(){ for (int i = 0; i < Companions.Count; i++) { //设置要到达的第一个未被占用的位置 int a = 0; while (!firstLine[a].GetComponent<Position>().isEmpty) { a++; } Companions[i].GetComponent<Companion>().objectToReach = firstLine[a]; Companions[i].GetComponent<Companion>().distanceFromTarget = Vector3.Distance(Companions[i].transform.position, firstLine[a].transform.position); firstLine[a].GetComponent<Position>().SetEmptyOrNot(false); //设置要到达的最接近的未被占用的位置 for (int j = 0; j < firstLine.Count; j++) { float dist = Vector3.Distance(Companions[i].transform.position, firstLine[j].transform.position); if (dist < Companions[i].GetComponent<Companion>().distanceFromTarget && firstLine[j].GetComponent<Position>().isEmpty) { Companions[i].GetComponent<Companion>().objectToReach.GetComponent<Position>().SetEmptyOrNot(true); Companions[i].GetComponent<Companion>().objectToReach = firstLine[j]; Companions[i].GetComponent<Companion>().distanceFromTarget = dist; firstLine[j].GetComponent<Position>().SetEmptyOrNot(false); } } }}
}
并编辑了同伴脚本:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Companion : MonoBehaviour{public GameObject objectToReach;public float distanceFromTarget;Transform target;// Start function not needed// Update is called once per framevoid Update(){ target = objectToReach.transform; //到达目标 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 1 * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, target.position, .01f);}
}
问题是现在当一个位置被选择后,它们不再更改目标…
回答: