Unity:如何让游戏对象到达最近的空闲位置

我正在尝试为同伴创建一个AI,让它们跟随玩家并定位到尚未被其他同伴占据的最接近的位置enter image description here

我在同伴上有一个脚本:

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);}

}

问题是现在当一个位置被选择后,它们不再更改目标…


回答:

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注