- Home /
Question by
rickycolon9540 · Nov 30, 2021 at 02:39 AM ·
programmingspawning problemsspawnpoints
Spawn gameobject in proper spawn position
I am making a strategy city builder game. When I spawn a certain building I want some archers to spawn along with it. For some reason when I spawn the building, the archers spawn in the center of the map rather than in the spawn position.
public class SpawnArchers : MonoBehaviour
{
//random position within spawn point
[SerializeField]
private float spawnRadiousMax = 1;
//spawn points
[SerializeField]
private Transform spawnPositionTransform;
//spawn position
private Vector3 spawnPosition;
[SerializeField]
private int remainingArcherSpawnAmount;
private float nextArcherSpawnTimer;
[SerializeField]
private GameObject archerPrefab;
private void Awake()
{
spawnPositionTransform.localPosition = spawnPosition;
}
private void Update()
{
Spawn();
}
private void Spawn()
{
if (remainingArcherSpawnAmount > 0)
{
//next enemy spawn timer
nextArcherSpawnTimer -= Time.deltaTime;
if (nextArcherSpawnTimer < 0f)
{
nextArcherSpawnTimer = UnityEngine.Random.Range(0f, .2f);
//spawns archer
PlayerArcher.Create(spawnPosition + UtilsClass.GetRandomDir() * UnityEngine.Random.Range(0f, spawnRadiousMax));
//1 less archer
remainingArcherSpawnAmount--;
if (remainingArcherSpawnAmount <= 0)
{
//
}
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, spawnRadiousMax);
}
}
Comment