Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Collision blocking for grid movement

Discussion in '2D' started by SondreKile, Oct 22, 2023.

  1. SondreKile

    SondreKile

    Joined:
    Dec 4, 2022
    Posts:
    5
    My 2D game limits movement to integers, like a grid. When moving a block, whatever is on top of it becomes a child, and follows its parent movement. But if a child is blocked by something unmovable, I want them to be unparented and not follow the parents movement. What would be the best approach to block movement?


    I have tried many different approaches, but none was satisfactory. I tried:

    1. OnTriggerCollision2D detects when the box is already inside the unmovable object. I can correct its position and unparent, but it does not look good.

    2. Similar approach with raycast outside boundaries, but the ruleset became to large, and hard to build on further

    3. Using Rigidbody and physics blocks the movement, but the physics glitches out everything. At least it blocks movement, avoid weird visuals.

    Help 1.jpg
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,112
    If you're in a grid, don't use collisions / colliders. Just track the logic in your grid.

    When you go to move, check if a piece is there to decide if you can move.

    If it is a potentially-movable piece, then check if the spot beyond it can be moved and do it.

    If you want an example of spawning stuff in a grid, checkout
    Bitmap2Grid
    or other scenes containing the word "Grid" in my MakeGeo project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo


    If you want an example of grid-based logic, checking the four neighbors around you, making decisions, check out my Match3 game, which comes with full source project in a link below it:

    https://kurtdekker.itch.io/match3-demo
     
    SondreKile and MelvMay like this.
  3. SondreKile

    SondreKile

    Joined:
    Dec 4, 2022
    Posts:
    5
    Thanks, that helped me in the right direction. You say I dont need colliders on my objects - is there another way other than raytracing to test what box is in the neighbours? I do some tests during movement, as the game is semi-turnbased.

    I use tilemaps for the stage, and gameobjects (boxes, players) placed at integerpoints in world position - no integrated grid.

    I didnt manage to look at the code you sent me, probably a beginner mistake on my part, sorry. I will update this post when I figure out the best solution for my game.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,084
    The tile map itself holds the information. There Tilemap API has plenty of methods to get tiles at certain indicies, and get said indicies from world positions: https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.html

    So before you move a block, you can check if the tile it would move into contains an immoveable block.
     
    SondreKile likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,112
    I guess you didn't read either:

    - the Match3 source code I posted above

    - the suggestion to look for scenes containing the word "grid" in the above repo (there are only 2)

    I'm not sure I will suggest anything else for you to not look at.
     
  6. SondreKile

    SondreKile

    Joined:
    Dec 4, 2022
    Posts:
    5
    I got dyslexia, sorry for the inconveniance. Didn't get the resources to load yesterday, but it works now, thanks a lot!
     
  7. SondreKile

    SondreKile

    Joined:
    Dec 4, 2022
    Posts:
    5
    Do you know if the tilmemap method is better than raytracing when it comes to performance?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,112
    You can find out for yourself! Start with the profiler (Window -> Analysis -> Profiler).

    Otherwise...

    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 you are gathering information at this stage. You cannot FIX until you FIND.

    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.

    Don't forget about the Frame Debugger either, available right near the Profiler in the menu system.

    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.

    "Software does not run in a magic fairy aether powered by the fevered dreams of CS PhDs." - Mike Acton
     
    SondreKile likes this.
  9. SondreKile

    SondreKile

    Joined:
    Dec 4, 2022
    Posts:
    5
    Wow, thanks you so much for the help, I got a lot of new things to check out that I never knew existed. Its not going to be a huge game, so I will avoid unnecessary optimizations unless it becomes a problem. You have been a huge help!
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,084
    In a project of your scope performance is likely irrelevant. Getting it working is more important.
     
    SondreKile and Kurt-Dekker like this.