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
- This topic has 27 replies, 4 voices, and was last updated 5 days, 6 hours ago by
Alp Apustaja.
-
AuthorPosts
-
April 24, 2025 at 4:01 pm #17998::
Thanks, to make the enemy collision I assigned them a 2D Rigidbody and excluded collisions with the player layer. This prevents both from being able to push each other around. Mass, Damping and Gravity remained the same.
Awesome Nathaniel. That’s great.It’s really neat because the enemy can still get caught up on props.
May 6, 2025 at 3:38 pm #18093::Thank you for this very good idea! I have a problem when assigning a 2D Rigidbody to an enemy. If the enemy is out of the screen boundary it will just disappear and never spawn again. Enemys with a 2D Rigidbody which should spawn from the SpawnManager won’t appear at all. It’s only when I use the 2D Rigidbody on an enemy. Any ideas what could cause this problem?
I completed part 24 of the series btw.
has upvoted this post. May 6, 2025 at 3:41 pm #18094::Hi Grim Rubbish!
I’m glad to hear you’re finding the series helpful, and congratulations on completing part 24!
The issue you’re experiencing with the 2D Rigidbody could be due to several factors. Here are some common things to check that might help resolve the problem:
-
Spawn Position: Make sure that the spawn position for the enemies is within the visible screen bounds. If the spawn position is set to a point that is off-screen, the enemy may be instantiated but not visible.
-
Rigidbody Settings: Check the properties of the Rigidbody 2D component. If you have the “Is Kinematic” option checked, it means the Rigidbody will not be affected by physics (including gravity), which can sometimes lead to unexpected behavior when spawning.
-
Z Position: In a 2D game, ensure that the Z position of the enemy GameObject is set correctly. If the Z value is too far from the camera’s plane, it may appear as if the enemy has disappeared.
-
Collider Setup: Ensure that the enemy has a collider component attached (such as BoxCollider2D or CircleCollider2D) and that it is not set to “Is Trigger” unless you intend for it to pass through other colliders.
-
SpawnManager Logic: Double-check the logic in your SpawnManager. Ensure that there are no conditions that might prevent the enemy from spawning when it has a Rigidbody. Review the spawning code to see if there are any checks or constraints that could affect the spawning of Rigidbody enemies.
-
Debugging: Use
Debug.Log()
statements in your spawning code to trace whether the spawn function is being called and if the enemies are being instantiated correctly.
If you check these aspects and still face issues, feel free to share more details about your SpawnManager code or enemy setup, and I’ll do my best to assist you further! Happy coding!
May 6, 2025 at 3:48 pm #18095::I noticed something: The Enemies with the 2D Rigidbody are still visible in the Editor screen but not in the game itself – weird…
May 6, 2025 at 3:51 pm #18096::Hi Grim Rubbish!
It sounds like you’re encountering an issue where the enemies are visible in the Unity Editor but not during gameplay. Here are a few things you can check to troubleshoot this issue:
-
Camera Settings: Ensure that your camera is correctly set up to render the layer that the enemies are on. Check the “Culling Mask” setting on your camera to make sure it includes the layer the enemies are assigned to.
-
Z Position: As mentioned earlier, make sure the Z position of the enemies is appropriate for your camera’s viewing plane. If the Z value is too far away or negative, the enemies may not appear in the game view.
-
Sorting Layers: If you’re using Sprite Renderers, check the sorting layer and order in the Sprite Renderer component. Ensure that the enemies are in a layer that’s rendered in front of the background and other objects.
-
Active State: Confirm that the enemy GameObjects are active in the hierarchy during gameplay. If they’re deactivated in the hierarchy, they won’t be rendered in the game view.
-
Game View Settings: Double-check the Game view settings to ensure that you’re not accidentally hiding layers or objects. Sometimes, the Game view can have different settings that might affect visibility.
-
Physics Settings: If you’ve set up any physics settings that might affect rendering or collision, review those to ensure they aren’t impacting the enemy’s visibility.
If you’ve checked all of these and are still having trouble, please provide more details about your enemy’s setup, including any relevant code or components, and I’ll be happy to help you further troubleshoot the issue!
has upvoted this post. May 6, 2025 at 3:54 pm #18097May 6, 2025 at 3:56 pm #18098::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:
-
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.
-
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
-
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.
-
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. May 6, 2025 at 4:03 pm #18100May 6, 2025 at 4:05 pm #18102::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:
-
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.
-
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. -
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.
-
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.
-
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. May 6, 2025 at 4:19 pm #18104::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. May 6, 2025 at 4:20 pm #18105::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. May 6, 2025 at 4:31 pm #18106::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. May 6, 2025 at 4:35 pm #18107::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 usingViewportToWorldPoint()
, 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. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: