使用Vector3.Distance方法时遇到问题。我要做的是让敌人随机地在地图中的某些点产生,当它看到玩家时,它将布尔变量设置为“true”(这意味着玩家已被看到)并且它开始产生每10秒(或多或少)距离玩家最近的点。
以下是代码:
if (!seen) {
TrisCoordinates tc = GetComponent<SpawnPoints>().GetRandomPosition();
go = (GameObject) Instantiate (boss.gameObject, new Vector3(tc.GetX (), tc.GetY(), tc.GetZ ()), Quaternion.identity);
} else {
Vector3 min = new Vector3(coords[0].GetX (), coords[0].GetY (), coords[0].GetZ ());
float dist = Vector3.Distance (min, player.position);
Vector3 temp;
for (int i = 0; i < coords.Count; i++) {
temp = new Vector3 (coords[i].GetX (), coords[i].GetY (), coords[i].GetZ ());
if (dist > Vector3.Distance (temp, player.position)) {
min = temp;
dist = Vector3.Distance (temp, player.position);
}
}
TrisCoordinates tc = new TrisCoordinates (min.x, min.y, min.z);
Instantiate (boss.gameObject, min, transform.rotation);
}
TrisCoordinates - &gt;保持三个坐标(x,y,z)并且具有吸气剂和放大器的类setter方法
SpawnPoints - &gt;保持可以产生敌人的所有可能位置的类
见过 - &gt;布尔变量,表示敌人是否看过玩家
在实践中,问题是在开始时敌人被正确地随机产生,当它看到玩家开始在最近的点产生但是当玩家进入森林时(有很多树)敌人开始在更远的位置产生,并且它不再在玩家附近产生。只有当玩家进入森林时才会发生这种情况,这可能是由某种计算或类似的东西造成的?
我还发布了列表的初始化和设置布尔变量的部分:
private List<TrisCoordinates> coords = new List<TrisCoordinates>();
...
coords.Add (new TrisCoordinates (200.901f, 1.67f, 386.03f));
coords.Add (new TrisCoordinates (222.328f, 1.67f, 386.03f));
coords.Add (new TrisCoordinates (338.672f,1.67f, 338.672f));
coords.Add (new TrisCoordinates (424.3f, 1.67f, 369.37f));
coords.Add (new TrisCoordinates (326.24f, 1.67f, 236.99f));
coords.Add (new TrisCoordinates (305.3f,1.67f, 259.3f));
coords.Add (new TrisCoordinates (280.29f, 1.67f,160.22f));
coords.Add (new TrisCoordinates (226.96f, 1.67f, 298.74f));
coords.Add (new TrisCoordinates (264.97f, 1.67f, 292.15f));
coords.Add (new TrisCoordinates (271.54f, 1.67f, 320.14f));
coords.Add (new TrisCoordinates (245.01f, 1.67f, 328.54f));
以下是敌人的完整代码:
void FixedUpdate () {
timer -= Time.deltaTime;
transform.LookAt (player);
Ray ray = new Ray (transform.position, transform.forward);
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.CompareTag ("Player")) {
if (hit.distance < 20) {
seen = true;
}
}
}
if (timer < 0) {
if (!seen) {
TrisCoordinates tc = GetComponent<SpawnPoints>().GetRandomPosition();
go = (GameObject) Instantiate (boss.gameObject, new Vector3(tc.GetX (), tc.GetY(), tc.GetZ ()), Quaternion.identity);
} else {
Vector3 min = new Vector3(coords[0].GetX (), coords[0].GetY (), coords[0].GetZ ());
float dist = Vector3.Distance (min, player.position);
//Debug.Log ("DISTANZA OLD = "+dist);
Vector3 temp;
for (int i = 0; i < coords.Count; i++) {
temp = new Vector3 (coords[i].GetX (), coords[i].GetY (), coords[i].GetZ ());
if (dist > Vector3.Distance (temp, player.position)) {
min = temp;
dist = Vector3.Distance (temp, player.position);
//Debug.Log ("DISTANZA NEW = "+Vector3.Distance (temp, player.position));
}
}
Debug.Log ("DISTANZA PRESA = "+Vector3.Distance (min, player.position));
TrisCoordinates tc = new TrisCoordinates (min.x, min.y, min.z);
//Debug.Log ("X = "+tc.GetX()+"; Y = "+tc.GetY ()+"; Z = "+tc.GetZ ());
Instantiate (boss.gameObject, min, transform.rotation);
}
Destroy (transform.gameObject);
}
}
所以,从理论上讲,只要敌人在一定距离看到玩家,它就会将变量设置为真,并且对于生成点中敌人的每次实例化,变量都设置为true,敌人应始终生成在离玩家最近的点。有时它有效,有时则不然;我真的不明白这个问题!
答案 0 :(得分:0)
不,森林不应影响您的距离计算。 Vector3.Distance函数只考虑你提供给它的两个位置,没有别的。 代码看起来像循环遍历coords []数组的每个元素,并将其与距离进行比较,每次都保持最低值:这看起来对我来说是正确的(尽管我会把它变成Vector3的数组)。 / p>
您的代码未显示的内容以及我怀疑问题所在的位置是coords []数组的设置方式。我们也不能看到那个数组是什么类。我怀疑你的问题在于这个数组的初始化。但是,如果没有看到那些代码,我也无法确定。