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. Dismiss Notice

Resolved Selecting Edges on a Grid

Discussion in 'Scripting' started by Bluspur, Mar 21, 2022.

  1. Bluspur

    Bluspur

    Joined:
    Dec 19, 2020
    Posts:
    8
    Hi so I am working on a small level editor for an Xcom like game. You can place prefabs down onto a grid via a mouse click and that all works very well and they are able to snap to the centre of a grid tile: See example below:

    My problem is that I want to be able to place "thin" walls between tiles of the grid, so I can simulate stuff like sandbags etc that don't occupy a full tile. However I am really struggling to figure out exactly how I would take a mouse click position, in world space, and use that to calculate which tile edge was selected.
    Red Blob Games covers the concept on their page about Grid Edges:
    https://www.redblobgames.com/grids/edges/

    I haven't been able to figure out how to implement something similar with C# and Unity.
    If anyone has worked on a similar issue I would really be interested in hearing how you were able to solve it. Although I'm sure there are people out there who are better at working with geometry than me who might have some suggestions on how to tackle this issue.
     
    Olleus likes this.
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    In this case you aren't making use of the line data but what is often done with this sort of system you're creating little empties on each corner of the wall you are placing. You would then have the empties snap to the appropriate corners rather than the edges themselves. I think this method is also called vertex snapping and it's what unity has in it's editor as an option when you're placing stuff but the problem is when you use those search terms it doesn't really bring much up in the way of how to do it in runtime.

    There are a few design decisions to be made when it comes to grid snapping and how you want it done, you could potentially get away with just reading off one empty and have it find the closest vertex on the grid as you move the mouse as a basic way of getting started.
     
    Bluspur likes this.
  3. Bluspur

    Bluspur

    Joined:
    Dec 19, 2020
    Posts:
    8
    So I'm pretty confident with how to physically place the prefabs in the scene and how to get them to snap. I'd probably stick to a single empty or more probably just make sure that the origin of any wall is at one of the ends, so I can just pivot it as needed.
    What I'm really struggling with is how to take a position on a grid, do some work on it, and come out with a number that could tell me which edge is closest.
    I am currently working with the assumption that a tile is responsible for two edges, so the tile at 0,0 would also handle the content at 0,0,E and 0,0,N and would reference its neighbours if it needed to know about the other edges.
    For now I am going to see if there is any of way of using each edges center position to determine which is closest, but the fact that there is a half tile offset every other column or row is already making that harder than I bargained for.
    I'm sure there is a simpler way of doing this, and I might just not be finding the right search terms,
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Yeah actually figuring out how to grab the data is a constant problem for me as well but it's something you have to think about a lot in programming when it comes to mechanics like this. The main thing to look at is how you are even generating your grid. Having a quick glance at your resource you're relying on for this there's a pretty decent mathematical explanation here on that topic.

    In this example they give, they tell you how to store the corners and the edges into a readable format for coding. I was thinking of corners because points I would've thought would be generally easier to work with in terms of code. You could maybe convert the array into a list or maybe an array would be okay, then grab the closest point to your mouse and use vector3.distance for that.

    This is actually quite a good article on the topic you've found, it's just a matter of studying it and understanding the maths they've given.
     
  5. Bluspur

    Bluspur

    Joined:
    Dec 19, 2020
    Posts:
    8
    So if anyone finds this and is looking to do something similar. Amit Patel from Red Blob Games posted a rather neat explanation on Twitter than runs through a quick mathematical way of determing the closest edge to a point:
    https://twitter.com/redblobgames/status/1494035888749223938
    I was able to use the explanation he laid out and have now got a working solution!
     
    Mr-Jun and Lethn like this.
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Nice one, after that it's all about designing your artwork to snap neatly when it comes to grids but you seem to have grasped that already.