- Home /
Random Respawn,Random Spawn
Currently, I'm making a shooting game. I want to respawn enemies randomly. This is the code i've made:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RespawnManager : MonoBehaviour {
public GameObject obj;
public Transform respawnTr;
void Start () {
StartCoroutine(RespawnEnemy());
}
IEnumerator RespawnEnemy ()
{
while (true) {
yield return new WaitForSeconds (0.7f);
float range = Screen.width / Screen.height * Camera.main.orthographicSize;
float random_num = Random.Range (-range, range);
Instantiate (obj, respawnTr.position + new Vector3(random_num, 0, 0), Quaternion.identity);
}
}
}
However, when I run the program, the enemies just respawns at one place. How should I solve this problem????
Answer by tormentoarmagedoom · Jun 18, 2018 at 07:41 AM
Good day.
I see your position random is based on screen with, size and orthographic size.
I dont know whats the result of "float range = Screen.width / Screen.height * Camera.main.orthographicSize;"
Can you put this 2 lines right before the Instantiate line and say us what you get in the Console?
Debug.Log ( "range = " + float range = Screen.width / Screen.height * Camera.main.orthographicSize);
Debug.Log (float random_num = Random.Range (-range, range));
But, I recommend to don't use this to make a random position. is better if you do random range from numbers, like:
float random_num = Random.Range (-20, 20);
Bye!
Hmm... I've got 0 for my values.... How should I fix this problem?? Thank You :)