- Home /
Problems with spawnpoints
Hey I'm programming a game where the player has to shoot three enemies in a map. If the player shot all of the three enemies, the spawnpoints of them will be changed and they will spawn at the new place. But it doesn't work properly. So if I kill the enemies the player has to trigger the new area and then the enemies will spawn. But if the trigger is on the enemies are spawn always, for a few frames, at the spawnpoint which where one before. I have just 3 spawnpoints for the enemies and I'm changing their positions. And after few frames they have the correct position. I don't understand this, because I have looked in my code and I change the spawnposition first and then activate them. My guess is that because I am doing this: enemies.ActivateSelf(false) they need a little bit to realise that their position has changed. But I'm not sure. I wanted to use the IEnumerator but do you guys have a better solution? Or is this the best? Here my code:
public void GiveAllEnemiesSameReward(float reward)
{
if (!enemyAgent.activeSelf && !enemyAgent2.activeSelf && !enemyAgent3.activeSelf)
{
if (!isWallMoved)
{
MovingWalls();
isWallMoved = true;
}
if (triggerComp.isTriggered)
{
SetEnemysActive();
Debug.Log("Enemies active");
triggerComp.SetIsTriggered(false);
isWallMoved = false;
isTimeIncreased = false;
isScoreIncreased = false;
isEnemyDone = false;
}
}
}
public void MovingWalls()
{
countLevel++;
switch (countLevel)
{
case 1:
movingWall1.transform.localPosition = spawnPointWall3.transform.position;
spawnPointPlayer.transform.localPosition = new Vector3(27.2f, 2, 2.6f);
spawnPointEnemy1.transform.localPosition = new Vector3(62.4f, 2, 14);
spawnPointEnemy2.transform.localPosition = new Vector3(62.4f, 2, 1.9f);
spawnPointEnemy3.transform.localPosition = new Vector3(62.4f, 2, -12.3f);
Debug.Log("Spawnpoint verschoben");
break;
}
I know it's not good to hardcode the positions but I didn't know how to do it in a better way.
Not sure whether that's the problem you're looking for, but you are missing a bracket in your switch statement.
Answer by ray2yar · Dec 27, 2018 at 02:07 AM
I'm not even sure what your problem is exactly...
You could try turning off the enemy renderers and colliders and leaving the objects themselves active.
You might also try setting the enemies active before changing their locations.
Maybe use .activeInHeirachy instead of .activeself
Definitely use .SetActive(true) and .SetActive(false) instead
I have provided a code example below to help with avoiding hard coding. You could setup an array of spawn locations and an array of enemies and have them choose a new spawn point from that array randomly (or by a logical method) You'll still have to code in the actual valid points in the array or use an array of empty gameobjects to represent. In the code I provided below you can use the inspector drag you enemies into the array of enemies and some empties as spawn spots in their array. The List of used locations might not update fast enough, so you might need to replace that with an array as well.
If you could clarify the actual issue you're having I'd be glad to help.
public GameObject[] SpawnLocations;
public GameObject[] EnemyAgents;
List<int> UsedPoints;
GameObject[] Enemies;
private void Start()
{
GameObject[] SpawnLocations = GameObject.FindGameObjectsWithTag("SpawnPoint");
}
void OnSpawn()
{
int location=0;
UsedPoints.Clear();
foreach(GameObject GO in EnemyAgents)
{
bool doneFinding = false;
do
{
location = Random.Range(0, SpawnLocations.Length);
if (!UsedPoints.Contains(location)) {
doneFinding = true;
UsedPoints.Add(location);
GO.transform.position = SpawnLocations[location].transform.position;
GO.SetActive(true);
}
} while (!doneFinding);
//be sure you have plenty of spawnlocations or this loop may not resolve
}
}