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 3 months, 4 weeks ago by
Alp Apustaja.
-
AuthorPosts
-
April 22, 2025 at 6:34 pm #17978::
Hello,
I am currently working through the tutorial series and I am early on in the series adding enemies and weapons etc… I was just wondering if in the later videos, the enemies will mimic collisions in VS. I noticed in VS that the enemies collide with one another and push other enemies into the player but never push the player directly.
I can imagine lots of enemies with lost of collisions could be quite taxing? I am new to Unity and C# but would be interested to know if this is planned/coming/covered?
April 22, 2025 at 6:40 pm #17979::Hello Nathaniel,
It’s great to hear that you’re diving into Unity and working through the tutorial series! Regarding your question about enemy collisions and their interactions with the player, it really depends on how the tutorial is structured and what features are planned for later videos.
In general, if enemies are set up to collide with each other, they can indeed push each other around, which can create interesting dynamics in gameplay. However, to prevent performance issues, especially when you have many enemies on screen, you’ll likely want to consider the following:
-
Collision Layers: You can use collision layers to manage which objects should interact with each other. For instance, you might want enemies to collide with each other but not with the player, or vice versa.
-
Physics Settings: Unity has various physics settings that you can tweak to improve performance. You can adjust the physics timestep, the number of collision checks, and other settings depending on your gameplay needs.
-
Rigidbody Settings: Make sure to configure the Rigidbody components on your enemies properly. For instance, using
Kinematic
for objects that shouldn’t be affected by physics can help reduce unnecessary calculations. -
Optimization Techniques: Techniques like object pooling for enemies can reduce instantiation overhead, which can be beneficial if you have a lot of enemies entering and exiting the scene frequently.
As for your expectation about whether these topics will be covered in the tutorial series, I would recommend checking the video descriptions or community forums related to the series. Sometimes, tutorial creators will outline what topics will be included in future videos.
Feel free to ask if you have any specific questions about implementing these features in Unity!
April 22, 2025 at 6:46 pm #17980::Thank you,
I’d like to know more about implementing collision layers so that I can make the enemies collide with one another but not push the player.
April 22, 2025 at 6:50 pm #17981::Sure, Nathaniel! Implementing collision layers in Unity is a great way to control how different objects interact with each other. Here’s how you can do it:
Step 1: Set Up Collision Layers
-
Open the Layer Inspector:
- In Unity, go to the top right of the Inspector window and find the “Layer” dropdown. Click on it.
-
Add New Layers:
- At the bottom of the dropdown, click on “Add Layer…”
- You can create new layers such as “Enemies,” “Player,” or any other layer you might need.
-
Assign Layers to GameObjects:
- Select your enemies in the hierarchy, and in the Inspector, set their layer to “Enemies.”
- Select your player GameObject and set its layer to “Player.”
Step 2: Configure the Physics Matrix
-
Open Project Settings:
- Go to
Edit
>Project Settings…
from the top menu.
- Go to
-
Navigate to Physics:
- In the Project Settings window, find the “Physics” section.
-
Edit Collision Matrix:
- You’ll see a matrix that displays all the layers. Here, you can adjust which layers should collide with one another.
- For example, if you want enemies to collide with each other but not with the player, ensure that the checkbox between the “Enemies” and “Player” layers is unchecked. You can leave the checkbox checked between the “Enemies” layer and itself.
Step 3: Testing Collisions
- Run Your Game:
- Play your game in Unity and observe the interactions. Enemies should collide with each other, but they will not push the player.
Additional Tips:
- Rigidbody Settings: Make sure that your player and enemies have Rigidbody components attached. For the player, you might want to set it to
Kinematic
if you don’t want physics forces to affect it. - Debugging: If collisions aren’t working as expected, use
Debug.Log()
to track collision events or check the colliders to ensure they are set up properly.
This setup will allow you to have more control over how your game objects interact, improving both performance and gameplay experience. If you have any more questions or need further clarification, feel free to ask!
April 22, 2025 at 8:31 pm #17982::I am currently working through the tutorial series and I am early on in the series adding enemies and weapons etc… I was just wondering if in the later videos, the enemies will mimic collisions in VS. I noticed in VS that the enemies collide with one another and push other enemies into the player but never push the player directly.
A simple way to mimic the behaviour in Vampire Survivors that Alp mentioned would be to increase the Mass of the player’s Rigidbody. This will make enemies less able to push the player around when the player is stationary.I haven’t tried this myself, but if it works it would be a really simple fix. Perhaps you can try it and let me know how it goes? The series doesn’t try to mimic the collisions in Vampire Survivors — an oversight on our part.
I can imagine lots of enemies with lost of collisions could be quite taxing? I am new to Unity and C# but would be interested to know if this is planned/coming/covered?
Collisions shouldn’t be an issue. Unity itself has various optimisations to make the physics as fast as possible. Of course, no matter how optimised the game is, it will always break at a certain number of enemies on screen. Vampire Survivors’ way of solving this is simply to limit the maximum number of enemies on screen (to 300 if I recall correctly).If you would like to performance maxx this more, you can use a Circle Collider 2D for everything (enemies, player, pickups, projectiles, etc.) — they are the fastest among all the 2D colliders.
@alp.apustaja can you provide an explanation for why Circle Collider 2D is faster than all the other colliders in Unity?
April 22, 2025 at 8:35 pm #17983::Based on the reference information provided, I can confirm that the tutorial series discusses using Circle Collider 2D as a method to reduce collision jankiness. Specifically, in the reference information under section “3. Fixing collision jankiness”, subsection “b. Rounding out colliders”, it states:
“The Circle Collider 2D will greatly reduce the chance of an enemy hitting the object at a right angle and getting stuck.”
However, the reference information does not provide a detailed explanation for why Circle Collider 2D is faster than other colliders. Therefore, I must respond with:
NOT_FOUND
April 22, 2025 at 8:40 pm #17984::Based on the reference information provided, I can confirm that the tutorial series discusses using Circle Collider 2D as a method to reduce collision jankiness. Specifically, in the reference information under section “3. Fixing collision jankiness”, subsection “b. Rounding out colliders”, it states:
Lolwut I need to fix you with a better brain.“The Circle Collider 2D will greatly reduce the chance of an enemy hitting the object at a right angle and getting stuck.”
However, the reference information does not provide a detailed explanation for why Circle Collider 2D is faster than other colliders. Therefore, I must respond with:
NOT_FOUND
I got this from Gemini instead:
Circle colliders are generally faster than other collider shapes (like box, polygon, etc.) because collision detection with a circle involves a simpler calculation: checking the distance from a point to the center of the circle and comparing it to the radius. Other shapes require more complex calculations to determine if there’s an overlap.
Here’s a more detailed explanation:
Circle/Sphere Collision: Collision detection with a circle or sphere involves calculating the distance between the center of the circle and the other object’s point or center. If this distance is less than or equal to the sum of the radii, a collision occurs. This is a single calculation.
Other Shapes: For more complex shapes like boxes or polygons, the collision check requires multiple calculations. For example, with a rectangle, you might need to check for overlap on multiple sides or vertices. This process can be more computationally expensive.
Complexity: The complexity of the shape directly impacts the complexity of the collision check. A circle, with its single radius, is the simplest shape for collision detection, leading to faster calculations.
Optimisation: While the difference in speed between circle and other colliders might be minimal in some cases, it can become significant when dealing with a large number of objects and collisions, according to a Reddit post.
April 22, 2025 at 9:13 pm #17985::A simple way to mimic the behaviour in Vampire Survivors that Alp mentioned would be to increase the Mass of the player’s Rigidbody. This will make enemies less able to push the player around when the player is stationary. I haven’t tried this myself, but if it works it would be a really simple fix. Perhaps you can try it and let me know how it goes? The series doesn’t try to mimic the collisions in Vampire Survivors — an oversight on our part.
Hi Terence, thanks for your response and thank you for making the video series. I am having a blast following along. I hope to see more soon. I will try this technique and let you know how I get on. I think a large part of what makes VS different and fun is how the enemies bounce off each other but not the player. The player is responsible for positioning and makes death feel deserved, not cheesed. This also allows for enemies to bundle up and get pushed onto the player as more come on screen.
Collisions shouldn’t be an issue. Unity itself has various optimisations to make the physics as fast as possible. Of course, no matter how optimised the game is, it will always break at a certain number of enemies on screen. Vampire Survivors’ way of solving this is simply to limit the maximum number of enemies on screen (to 300 if I recall correctly). If you would like to performance maxx this more, you can use a Circle Collider 2D for everything (enemies, player, pickups, projectiles, etc.) — they are the fastest among all the 2D colliders.
Do you have any plans to add optimizations for amount of enemies on screen in the future?
Once again thank you, I am planning to join the Patreon soon to give back for the fantastic videos. It would be great if you are working on more in this series.
has upvoted this post. April 22, 2025 at 9:16 pm #17986::Hi Nathaniel!
I’m glad to hear you’re enjoying the video series! Your insights about the mechanics in Vampire Survivors are spot on, and they can really enhance the gameplay experience.
Regarding your question about optimizations for the number of enemies on screen, while I can’t speak for the creators directly, it’s common practice in game development to consider performance optimizations as the project evolves. Here are a few common techniques that might be covered or could be beneficial for handling large numbers of enemies:
-
Pooling System: Instead of constantly creating and destroying enemy instances, you can implement an object pooling system. This reuses enemy objects, which can significantly reduce the overhead of instantiation.
-
Culling: Implementing culling techniques can help ensure that only enemies within the camera view are active. This can save processing power by not calculating physics for off-screen enemies.
-
Level of Detail (LOD): You can decrease the complexity of the enemy models or their behaviors based on their distance from the player. This can help maintain performance while keeping the game visually appealing.
-
Limiting Active Enemies: As you mentioned, limiting the maximum number of enemies on screen is a simple but effective way to manage performance.
It’s great to hear that you’re considering joining Patreon to support the creators! The more feedback they receive, the better they can tailor future content to the community’s needs. If you have any more questions or need help with specific implementations, feel free to ask. Happy developing!
April 22, 2025 at 9:21 pm #17987::Hi Nathaniel!
Thank you for your kind words about the video series! It’s great to hear that you’re enjoying it and finding it helpful. Your insights about the gameplay mechanics in Vampire Survivors are spot on, and I agree that the way enemies interact with the player and each other adds a unique layer to the experience.
Regarding your question about optimizations for the number of enemies on screen, while I can’t confirm specific plans, it’s always important for developers to consider performance as they create game mechanics. Many developers use various techniques to manage the number of active enemies, such as:
-
Culling Off-Screen Enemies: Ensuring that only enemies within the camera view are processed can significantly reduce performance overhead.
-
Implementing Object Pooling: This technique allows for reusing enemy instances rather than creating and destroying them, which can help maintain performance as the game scales.
-
Limiting Active Enemies: As you mentioned, setting a maximum number of enemies on screen is a common practice to maintain performance.
-
Using Simple Colliders: As suggested, using Circle Collider 2D for enemies and other objects can help with performance due to their simpler calculations.
If you have specific features or optimizations you’d like to see, I recommend sharing that feedback with the creators, as they often appreciate hearing from the community about what players want.
Thank you again for your support, and I hope you have fun implementing the changes! If you have more questions or need assistance, feel free to ask. Happy developing!
April 23, 2025 at 7:39 pm #17992::Do you have any plans to add optimizations for amount of enemies on screen in the future?
Once again thank you, I am planning to join the Patreon soon to give back for the fantastic videos. It would be great if you are working on more in this series.
No. I don’t see how else we can significantly improve how the game handles large numbers of enemies on screen. That may change if I get ideas in the future, though. Let me know if you find anything!
Thank you for your kind words as well my friend.
April 24, 2025 at 5:21 am #17993::Hello all,
I believe I have this mostly figured out.
Here is my enemy movement code:
using UnityEngine; public class EnemyMovement : MonoBehaviour { EnemyStats enemy; Transform player; SpriteRenderer spriteRenderer; void Start() { enemy = GetComponent<EnemyStats>(); player = FindFirstObjectByType<PlayerMovement>().transform; spriteRenderer = GetComponent<SpriteRenderer>(); } void Update() { transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemy.currentMoveSpeed * Time.deltaTime); if (player.position.x < transform.position.x) { spriteRenderer.flipX = true; } else { spriteRenderer.flipX = false; } } void LateUpdate() { float offset = transform.position.x * 0.001f; spriteRenderer.sortingOrder = Mathf.RoundToInt(-(transform.position.y + offset) * 100); } }
This includes amendments to allow for sprite flipping based on direction and a LateUpdate to sort order of sprites and reduce flickering.
To make this work, add a 2D Rigidbody to your enemies and set to Dynamic, Exclude the Player layer.
Next for the player, do the same but exclude nothing. You should already have one on the player if you’ve been following the series.
You need to ensure the Player is on it’s own layer individual to the enemies for this to work.
Thanks and sorry if any of this is done incorrectly or badly. I have been using Unity for 4 days haha.
April 24, 2025 at 11:00 am #17995::The code looks great Nathaniel. Especially if you’ve only been using Unity for 4 days. Modding existing games or implementing your own feature outside of tutorials are the quickest ways to learn (I’m not sure if you’re a coder, but if you are you should probably already be aware of this).
May I ask how you made the enemy collision? Did you just increase the Mass of the player character?
April 24, 2025 at 2:20 pm #17996::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.
It’s really neat because the enemy can still get caught up on props.
has upvoted this post. April 24, 2025 at 2:27 pm #17997::Based on the reference information provided, specifically the section on “3. Fixing collision jankiness”, this approach aligns with the tutorial’s recommendations. The reference information discusses using Layer Collision Matrix and mentions that you can disable collisions between certain layers.
The comment about enemies getting caught up on props also relates to the tutorial’s suggestion of using Circle Collider 2D to reduce clipping and collision issues with props.
Would you like to share more details about how the props are set up in your scene?
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. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: