How to make the enemy’s location is randomly appear?
Appears from both sides of the screen
And when out of range of the screen will be destroyed?
How can I do?
Here is my advice:
-
You could let your enemies spawn at completely arbitrary points, and that may be useful for some games, but that is easy to implement (
Instantiate(enemy, new Vector3(Random.Range(), 0, Random.Range() ), Quaternion.Idenity)
) so I will give an example of the slightly more complicated model. I would suggest having a collection of SpawnPoints then just randomly picking one. This will give your more control to make sure that enemies are spawned in reasonable places. So, you want to create a collection of Spawn points. Just place empty GO’s around the scene where you want them.EnemySpawner.js var enemyCount = 3; var actualCount = 0; var enemyPrefab : GameObject; var spawnPoints : SpawnPoint[]; function Start () { spawnPoints = FindObjectsOfType(SpawnPoint) as SpawnPoint[]; //Create a script called spawnpoint and attach it to your spawnpoints. while(actualCount < enemyCount) { actualCount++; pos : Vector3 = spawnPoints[Random.Range(0 , spawnPoints.Length)].transform.position; CreateEnemy(pos); } } function CreateEnemy (spawnPoint : Vector3) { Instantiate(enemyPrefab, spawnPoint, Quaternion.Identity); } //Note that this is very simple and I didn't really check it so there is a good chance there is a minor mistake.
-
Then Random.Range() should make sure that the spawn points will be used equally so you won’t have to worry about getting a variety. You might want to yield the value a frame to make sure the random number generator doesn’t take the same seed twice in a row.