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 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #17087
    Terence
    Level 32
    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.

    #19773
    Daniel
    Level 3
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    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.
    #19776
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::
    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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: