Forum begins after the advertisement:
[Part 22] Enemies are not spawning / spawning behind the camera
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 22] Enemies are not spawning / spawning behind the camera
- This topic has 2 replies, 2 voices, and was last updated 3 weeks, 1 day ago by
Terence.
-
AuthorPosts
-
January 10, 2025 at 12:40 am #17087::
Hi everyone, if you run into this issue where the enemies appear to not be spawning, they are likely on the same z-coordinate as your camera, and so cannot be seen.
If this is the case, please update the
GeneratePosition()function on yourSpawnManagerto specify the z-coordinate:// Creates a new location where we can place the enemy at. public static Vector3 GeneratePosition() { // If there is no reference camera, then get one. if (!instance.referenceCamera) instance.referenceCamera = Camera.main; // Give a warning if the camera is not orthographic. if (!instance.referenceCamera.orthographic) Debug.LogWarning("The reference camera is not orthographic! This will cause enemy spawns to sometimes appear within camera boundaries!"); // Generate a position outside of camera boundaries using 2 random numbers. float x = Random.Range(0f, 1f), y = Random.Range(0f, 1f); // Then, randomly choose whether we want to round the x or the y value. switch (Random.Range(0, 2)) { case 0: default: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(Mathf.Round(x), y, 10)); case 1: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(x, Mathf.Round(y), 10)); } }If the value of 10 doesn’t work, you’ll have to play around with and try other values.
June 23, 2026 at 1:58 am #19773::I made a change so I don’t have to “play” with the value manually and will work if the camera’s Z value changes. Posting here in case someone finds it interesting.
// Then, randomly choose whether we want to round the x or the y value float cameraZOffset = -instance.referenceCamera.transform.position.z; // Ensures that the spawned entity's Z is 0 switch (Random.Range(0, 2)) { case 0: default: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(Mathf.Round(x), y, cameraZOffset)); case 1: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(x, Mathf.Round(y), cameraZOffset)); }has upvoted this post. June 23, 2026 at 8:31 pm #19776::I made a change so I don’t have to “play” with the value manually and will work if the camera’s Z value changes. Posting here in case someone finds it interesting.
// Then, randomly choose whether we want to round the x or the y value float cameraZOffset = -instance.referenceCamera.transform.position.z; // Ensures that the spawned entity's Z is 0 switch (Random.Range(0, 2)) { case 0: default: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(Mathf.Round(x), y, cameraZOffset)); case 1: return instance.referenceCamera.ViewportToWorldPoint(new Vector3(x, Mathf.Round(y), cameraZOffset)); }Fantastic. Thanks Daniel.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: