How do I overlay a grid movement system on top of a UE4 landscape

I have a landscape created in UE4 which I then want to overlay a basic grid movement system like this:

258677-exammple.png

All the examples I can find utilize blueprints and also incorporate some sort of procedural spawning system all I want to do is in a nutshell, create and sculpt my landscape, and then implement a click to move grid based movement system on top of that. Any suggestions would be great thanks.

This sort of thing can take a lot of detail to fully discuss and get totally right, but here are the very basics of how you might go about it.

You can give the impression of grid-based movement by “snapping” world space locations to a value that is rounded to the nearest multiple of whatever grid size you desire. (The technical term for this technique is ‘quantization’.)

For example, if the player is moving the cursor over the landscape and you want to highlight a grid square under the cursor’s location that is 1 meter across, you first need to round the cursor’s world space location (quantize it) to the nearest multiple of 100 units (100 units = 1 meter in UE). You’ll use this rounded location as the new location for whatever means you’re using to highlight the ground, such as a decal actor that is projecting downward. This way, the cursor will still move smoothly, but the highlighted ground location underneath will appear to “snap” to the grid.

If you want to force the player character to also stick to the grid, you’ll also need to quantize the player’s location, or at least force the player to navigate from quantized locations to other quantized locations. So, in other words, if using a navigation system, make sure to quantize any location that the player is allowed to navigate to. (This may include the waypoints along the way to a navigation destination, depending on how you want player character movement to appear.)

Anyway, by quantizing many of the location variables that your players interact with, you’ll give the impression that a worldwide grid system exists, even though all you’re doing to achieve this is very simple math operations.

Hope that makes sense!

Hi thanks for the response. Your answer makes sense but you wouldn’t have an example I could see with it? For now I’ll continue trying to put your answer into code. Thanks!

I’ll try to more fully explain some of the individual pieces, since a fully realized example would be too much to cover in this comment box.

You can quantize your vector locations using the FVector::GridSnap(…) function. Just provide it a float value to tell it what size you want each grid square to be. (For example, 100 units, a.k.a. 1 meter.) This will enable you to easily convert any standard 3D location into one that respects your grid.

To highlight a square on the ground, you have several options, but one easy approach is to use a Decal actor, pointed straight down, with a material that creates a square border/shape of your liking.

To get this grid-highlighting decal actor to snap to the grid under the cursor, you can use a line trace to determine the 3D point where the cursor is “touching” the ground, then GridSnap() this location to quantize it, and finally move the decal actor to this quantized location.

If you’re performing the trace in a subclass of APlayerController, you can take advantage of the very convenient GetHitResultUnderCursor(…) function. For example:

FHitResult Hit;
GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, false, Hit);
if (Hit.bBlockingHit)
{
    // The projected 3D location is stored in Hit.Location, which we can
    //  quantize using FVector::GridSnap() and a grid size of 100 units
    FVector QuantizedCursorLocation = Hit.Location.GridSnap(100.f);
    ...
}

As for constraining player character movement to the grid, for starters, just try feeding the pathfinding system with grid-snapped locations. This, combined with the cursor highlight, should begin to sell the effect you’re looking for.

There’s much more that can be done, but that should get things started. I hope this makes sense, and sheds more light on the subject!