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

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #17087
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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 your SpawnManager to 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.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: