Forum begins after the advertisement:


[General] Question about enemy collision in the series

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [General] Question about enemy collision in the series

Viewing 8 posts - 21 through 28 (of 28 total)
  • Author
    Posts
  • #18097
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    Thank you, what should be the recommended Z Position for the enemys?

    has upvoted this post.
    #18098
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    In a typical 2D game setup in Unity, the Z position of your enemies (and other GameObjects) does not need to be very complex, but it is important for ensuring that they appear on the screen as intended. Here are some recommendations:

    1. Z Position for 2D: A common practice is to keep the Z position of all your 2D GameObjects (including enemies) at 0 or a small negative value, like -1. This ensures they are rendered in front of the camera, which is usually at a Z position of 0.

    2. Layering: If you have multiple layers (for example, background, player, enemies, etc.), make sure to adjust the Z position accordingly. For instance:

      • Background objects: -2 or -1
      • Player: 0
      • Enemies: 0 or a similar value to the player
      • UI elements: 1 or higher
    3. Camera Setup: Ensure that your camera is set to an orthographic projection for 2D games, as this will render all objects at the same Z value uniformly without perspective distortion.

    4. Sorting Layers: If you are using Sprite Renderers, also utilize sorting layers to manage which objects are drawn in front of others. This way, even if the Z positions are the same, you can control the rendering order.

    Make sure to test the visibility of the enemies in the game view after adjusting their Z positions, and if you have a specific setup in mind, feel free to share more details!

    has upvoted this post.
    #18100
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    I set the Z Position as intended, but it didn’t work. I’m giving up… xD

    has upvoted this post.
    #18102
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    I understand how frustrating that can be, Grim Rubbish! It’s easy to feel overwhelmed when things aren’t working as expected, but don’t give up just yet! Here are a couple of additional troubleshooting steps that might help:

    1. Check Active State in Play Mode: While in Play Mode, make sure the enemy GameObjects are still active. You can check this in the Hierarchy window. If they are inactive (greyed out), they won’t appear in the game.

    2. Debug.Log Statements: Add some Debug.Log() statements in your spawning code to see if the enemies are being instantiated correctly when the game starts. This will help confirm if the issue is with instantiation or rendering.

    3. Test with Simple Objects: As a test, create a simple GameObject (like a cube or sphere) with a 2D Rigidbody component and see if it appears in the game. This can help you isolate whether the issue is specific to your enemy setup or something else in the scene.

    4. Resetting the Scene: Sometimes, simply resetting the scene or starting a new scene can resolve unexpected behavior. If you’re comfortable doing so, try creating a new scene and setting up the enemy again.

    5. Unity Console: Check the Unity Console for any error messages or warnings that might indicate what’s going wrong. Sometimes there are clues there that can lead you to a solution.

    Remember, troubleshooting is a normal part of game development, and it might just take a few more tweaks to get things working! If you want to share any specific code or setup details, I’d be more than happy to help you dig deeper. Hang in there!

    has upvoted this post.
    #18104
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    I think I found out what causes this! Enemys with a Rigibody that spawn have a -10 on Z Position in Transform. All other enemies have 0 on Z Position in Transform. These are visible. How can I fix this problem, so that spawning Enemys with a Rigigbody also have 0 on Z Position in Transform?

    has upvoted this post.
    #18105
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    2
    ::

    To fix this, you’ll want to modify your enemy spawning code to explicitly set the Z position to 0 when instantiating enemies. In your SpawnManager or enemy instantiation script, when creating the enemy, use something like:

    Vector3 spawnPosition = new Vector3(xPos, yPos, 0f);
    GameObject enemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);

    This ensures that any spawned enemy will have its Z position set to 0, matching the visibility of your other enemies.

    and 1 other person have upvoted this post.
    #18106
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    It worked! For anyone having the same problem, here is the fix (line 145 – 156 in SpawnManager.cs). Z value can be 0 or higher. Cheers!

    // Generate a position outside of camera boundaries using 2 random numbers an fixed Z value.
    float x = Random.Range(0f, 1f), y = Random.Range(0f, 1f), z = 1;
    
    // 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, z));
        case 1:
            return instance.referenceCamera.ViewportToWorldPoint(new Vector3(x, Mathf.Round(y),z));
    }
    has upvoted this post.
    #18107
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    2
    ::

    Great job solving the Z-position visibility issue! The code snippet you shared demonstrates a good approach to generating spawn points with a consistent Z value. By setting z = 1 and using ViewportToWorldPoint(), you ensure that spawned enemies will be visible in the game view while still being positioned outside the camera’s initial view. This is a solid solution for managing enemy spawning in a 2D game.

    and 1 other person have upvoted this post.
Viewing 8 posts - 21 through 28 (of 28 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: