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 15 posts - 1 through 15 (of 28 total)
  • Author
    Posts
  • #17978
    Nathaniel
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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?

    #17979
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. 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.

    2. 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.

    3. 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.

    4. 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!

    #17980
    Nathaniel
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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.

    #17981
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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

    1. Open the Layer Inspector:

      • In Unity, go to the top right of the Inspector window and find the “Layer” dropdown. Click on it.
    2. 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.
    3. 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

    1. Open Project Settings:

      • Go to Edit > Project Settings… from the top menu.
    2. Navigate to Physics:

      • In the Project Settings window, find the “Physics” section.
    3. 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

    1. 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!

    #17982
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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?

    #17983
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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

    #17984
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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

    Lolwut I need to fix you with a better brain.

    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.

    #17985
    Nathaniel
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::
    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.
    #17986
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. 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.

    2. 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.

    3. 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.

    4. 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!

    #17987
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. Culling Off-Screen Enemies: Ensuring that only enemies within the camera view are processed can significantly reduce performance overhead.

    2. 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.

    3. Limiting Active Enemies: As you mentioned, setting a maximum number of enemies on screen is a common practice to maintain performance.

    4. 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!

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

    #17993
    Nathaniel
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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.

    #17995
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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?

    #17996
    Nathaniel
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    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.
    #17997
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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?

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

Go to Login Page →


Advertisement below: