Search Unity

Question Collisions

Discussion in 'Scripting' started by AusAdam, Jan 26, 2023.

  1. AusAdam

    AusAdam

    Joined:
    Jan 2, 2021
    Posts:
    62
    I am making a 2d tilemap farm game and trying to figure out the best way to do collisions.
    Should I do them in code and determine if the item that collided can hit them ie pickaxe and stone or would it be better to do it in the layer matrix and make it so that only the axe can collide with trees and pickaxe can collide with stones etc?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    You can do things either way. It really comes down to personal taste and how tightly you want to integrate your game with the Unity API, plus of course game design.

    If the only person who can pick up things is the player then it's easy for pickaxes to check if the player is close enough and the overall setup might be faster / simpler.

    Otherwise using the colliders (or triggers) along with layers to divide broad categories of things will always scale better, at the cost of tighter integration into Unity.
     
  3. AusAdam

    AusAdam

    Joined:
    Jan 2, 2021
    Posts:
    62
    yeah i have axes with trees / pickaxes with stones / scythes with grass/crops
    I figured layers would be the best way however I did forget to mention that it is for a mobile game so looking for the most optimized way to do this.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Then just use Unity's collisions / triggers. There's like ten billion tutorials already out there that are each a million times better than anything that could ever possibly be typed in this little box.

    Don't get all tangled up in optimization. That's a fools errand... resist it regardless of what you read online. If you start trying to be clever you will sink your own battleship fast and you will really hate gamedev.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.