I have a problem using Vector3.Distance method. I'm trying to make a game similar to Slender Man, but a little bit different; what I'm trying to do is to make the enemy spawn in certain points in the map randomly, and when it sees the player it sets a boolean variable to "true" (which means the player has been seen) and it starts to spawn every 10 seconds (more or less) in the nearest point from the player. Here is the code:
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 --> class which keeps three coordinates (x,y,z) and which has getters & setters
SpawnPoints --> class which keeps all the possible positions where the enemy can be spawned
seen --> boolean variable which indicates if the enemy has seen the player
In practice, the problem is that at the beginning the enemy is correctly randomly spawned, when it sees the player it starts to be spawned in the nearest points BUT when the player goes into the forest (there are a lot of trees) the enemy begins to be spawned in further positions and it doesn't spawn anymore near the player. This happens only if the player goes into the forest, could it be caused by some kind of computation or stuff like that?
Aucun commentaire:
Enregistrer un commentaire