Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Too Many 2D Colliders for Android Game

Discussion in 'Physics' started by bloodlydevil, Apr 27, 2024.

  1. bloodlydevil

    bloodlydevil

    Joined:
    Apr 15, 2023
    Posts:
    3
    I am making a game whose base game set up is taken from slither.io (it's basically a snake game where you eat food and try to kill another snake)


    The thing is the snake set up is like this->

    There is a head (2D Collider)
    A prefab of body (2d collider)
    And a script which on trigger creates a new snake body.

    A snake on average can have 50 body so 50 2d colliders and there are 100 enemy snakes with same set up so total colliders reach 5000 collider2d and then I have nearly 200 to 300 foods collider2d.

    So, in total i have 5500 colliders. It would not be an issue, but my game is supposed to be working on Android and my Game is not smooth in Android (provided I get 300 to 400 fps on pc)

    I have profiled the game and problem indeed lies in collider.

    I cannot think of a way to batch the colliders of a snake together because each body has collider which is a huge problem.
    Things I have tried ->
    Use spline instead of manual creation of body (spline cashed error-> i believe too many knots' elements are there in) and then use mesh generation by extension but it reduces the fps to 30 to 40 on pc.

    I think in the game (slither) they are only creating around 10 snakes apart from the player and are using shader to render the snake as whole but how are the collision detection working at such rate is beyond me.

    If you can provide any solution, then it would be helpful
     

    Attached Files:

    Last edited: Apr 27, 2024
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,564
    Because any physics involved is being used correctly and efficiently.

    The profiler is showing you that it's having to process you modifying the Transform directly for a total of 1172 colliders; something you should not be doing with physics. Even worse, you're modifying the Transform on a Collider2D that isn't attached to a Rigidbody2D causing it to be recreated (destroy/create).

    A collider without a Rigidbody2D is STATIC i.e. non-moving.

    A Rigidbody2D moves, colliders are attached to it. There are three body-types to control how it reacts. Add a Rigidbody2D and use its API to move.

    The general rules is, if you're modifying a Transform in physics to cause movement, you're doing it wrong.
     
    bloodlydevil likes this.
  3. bloodlydevil

    bloodlydevil

    Joined:
    Apr 15, 2023
    Posts:
    3
    Thanks for the Reply...


    This new knowledge created whole new problems for me->

    1. Would Attaching Rigidbody2D to each and every collider2d (namely 5000 Rigidbody2D) cause some sort of slow down (will try it now and profile it to see the effect but would love know of your thoughts on this )
    2. Is it possible to just add a single Rigidbody2D to the parent game object to reduce the overall count of Rigidbody2D(namely from 5000+ to 30) or will the collider2d sill count as static as it is a child
    3. If I in the end use the Rigidbody2D then the best way to move the object would be applying force(As far as other people have led me to believe) when in fact I am handling all the movement manually for greater control (Will using move position and set rotation of Rigidbody2D be any different the directly setting the transform)
    What I want->

    The Head Of The Snake needs to collide with other snake head or body (I Can Try to do Ray cast but Without collider their is no use of Raycast).
    I can try to check for the collision manually but I know for sure I will not make a good enough system for it (Could try to use DOTS but honestly it feels a bit of overkill)

    Some way to check for collision between objects without using Rigidbody2D or collider if possible(I know it feels i want two opposite things at one time)


    What about if I set Rigidbody2D to Kinematic will it give me all the controls(As much as I should have) for the movement of my object or will their be some overhead(I know I should not worry so must about micro management but if I can know about this then I will avoid it in future)(I can probably set all the other parameter to 0 so that Rigidbody2D will not effect my object more ) . BUT this whole approach feels conflicting to me to say that I am using a Rigidbody2D but not using it for movement and manually controlling it.


    Can using the physics of DOTS help me in any way(I have tried DOTS somewhat but not Its physics)


    Thanks for helping me so far..... and have a good day.
     
    Last edited: May 1, 2024
  4. bloodlydevil

    bloodlydevil

    Joined:
    Apr 15, 2023
    Posts:
    3
    Ok After Profiling it I have come to the following problem=>

    Its faster than previous built but not so fast as it should (I believe I it can be more optimized (Runs as S*** on phone))

    Should I create Mesh out of The Snake Body and use Mesh collider??

    I can not figure out how did the devs create it in Slither.io (As far as i believe in offline game their is at max 10 to 20 snake in game(i wanted to make at least 50 in whole map to give diversity)

    But i guess i can not Do it by creating it but must implement it in some way by falsely creating it

    How did the The devs of the game make the movement so smooth (I thought they used spline but when i tried it it proved to be horrible idea)
     

    Attached Files: