How to snap actor to procedural mesh grid closest vertex from mouse position?

Hello everyone,

I’ve been trying to make a wall building system for 2 weeks now and i’ve been struggling for the past 6 days trying to figure out how i can snap an actor (in this case it’s just an actor that contains an arrow) to the closest vertex on the grid compared to the mouse location.

The grid is a procedural mesh created like so:

i also have the camera logic to pan it around and a here is how things look for now inside a test level

2024-02-29 21-10-59.mkv (8.3 MB)

Hey @OlyxSystem!

So this is one of those “behind the curtain” things. The mesh you have is great for telling the player where they can place things. But it won’t have the functionality needed for this- you need to generate a series of points (which can be done procedurally) that the player can attach these things to. Don’t worry, they can be completely invisible, and be on the same actor as the mesh you made!

So just to guide you a bit, you’ll want to get fancy with an array and ForLoops, and creating scene nodes with those loops for the procedural part.

Then once you have that, to get the scene node (point) closest to the mouse, you want “Convert mouse location to world space” and then from THAT location, do a small sphere trace (so you’re doing a sphere trace around the mouse location then) and get all of the output actors and find the one with the smallest difference in distance from the original point! :slight_smile:

Well… while you could add “invisible” objects on the grid points and then do a sphere trace at the mouse projected location… I personally would slap myself if I ever did it that way! (lol, no offence)

Best way is to use just good old math.

First you convert the mouse location to world space, then you project the location to a plane (your grid is a plane in this case), you can use the line plane intersection node for this.
Then finally you can use something like the “vector snapped to grid” or “snap to grid (float)” node (along with some clamping if you don’t want to go outside the grid) to snap to nearest grid multiple.

If you need more help with the math, I could cook some example blueprint if you want.

I figured out a fairly weird way to do it, Basically i populated the grid like that

and made a function like so:

then added that to the event tick

2024-02-29 23-13-41.mkv (10.2 MB)

i’d be happy to see how your method turns out, let me know when you can cook something up!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.