- Home /
Spawning enemies at an offset
I have fixed spawn locations in my game and I would like to spawn my enemies within a range close to the spawn point, rather then the enemies coming straight from the one fixed point if you get me.
Is there a way I can do that with my current setup?
using UnityEngine;
using System.Collections;
public class spawnBall : MonoBehaviour
{
//The spawn points for the enemies
Vector3[] spawnPoints;
//Initialises the end of the array for the enemies
int endOfArray;
//The wait time before the coroutine will start
private int timeToBegin = 2;
//Determines the spawn time of the enemies
private float spawnTime = 2;
//The object spawned towards the enemy
public GameObject[] randomBall;
void Start()
{
//Finds all ovjects marked with the tag Spawn Points
GameObject[] objs = GameObject.FindGameObjectsWithTag("SpawnPoints") as GameObject[] ;
//Sets the spawnPoints varibale to the size of the objs array
spawnPoints = new Vector3[objs.Length] ;
//for loop which trasfers all data into the array
for(int i = 0 ; i < objs.Length ; i++)
{
spawnPoints[i] = objs[i].transform.position ;
}
//sets the end point for the array
endOfArray = spawnPoints.Length;
//Starts the coroutines for the enemies and missiles
StartCoroutine(Spawn());
}
// Update is called once per frame
void Update () {
}
//Instantiates a new enemy based on a time given by the players score
IEnumerator Spawn()
{
yield return new WaitForSeconds(timeToBegin);
while (true)
{
int prefeb_num = Random.Range(0,15);
//I'd like to create the offset here on the x-axis
Instantiate(randomBall[prefeb_num], spawnPoints[Random.Range(0,endOfArray)], Quaternion.identity);
yield return new WaitForSeconds(spawnTime);
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 100), "FPS: " + (int)(1.0f / Time.smoothDeltaTime));
}
}
http://docs.unity3d.com/ScriptReference/Random-insideUnitSphere.html
When you instantiate your object you can mix that to the position and adjust the y axis to the floor and the radius from the spot you wish to Spawn.
Something like this:
Vector3 Pos = spawnPoints[Random.Range(0,endOfArray)] + Random.insideUnitSphere * radius;
Vector3 SpawnPos = new Vector3(Pos.x, 0, Pos.z);
Instantiate(randomBall[prefeb_num], spawnPos , Quaternion.identity);
It's the best that comes to my $$anonymous$$d right now. I haven't tested it but I think it might work... I hope it helps you getting on the right track. Cheers!
Answer by Sindorej · Aug 16, 2014 at 06:39 PM
You can do something like this :
Float maxDistance = 15f;
Vector3 spawnpoint = spawnPoints[Random.Range(0,10)];
Vector3 target_pos = spawnpoint + new Vector3(Random.Range(0,maxDistance), Random.Range(0,maxDistance),Random.Range(0,maxDistance));
Instantiate(prefab, target_pos, prefab.transform.rotation);
You can of course change the range to negative, or make Y zero so they don't spawn in the air.
This works a charm, thanks a million for the help :)
Your answer
Follow this Question
Related Questions
How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers
Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer
Respawning Single Enemy at a Random Range 1 Answer
Instantiate a Prefab with click in a certain area 2 Answers
UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer