I have recently picked up Unreal Engine in an attempt to make a 2D game. Most of my hurdles have been hard to overcome, since all the help I find on 2D projects is always for Unity.
With that being said, there is one thing I can’t figure out.
How can I convert my mouse cursors location to a 2D plane so I can place a block? (Like you do in Terraria)
following online tutorials I am able to make block placing… but only in 3D… I can’t figure it out for 2D.
I’d normally gravitate towards using line plane intersection but it could become a nuisance when using an orthographic camera (are you going to?).
Instead, you could opt for something along the lines of:
a plane that moves with player, ignoring all collision but Visibility (or whichever channel you’ll dedicate for tracing) - ideally, you’d make a unique object channel
The GameplayStatics library contains a function “DeprojectScreenToWorld” which you should call from your PlayerController. By this you get the 2D cursor position as the 3D world position.
Since you are going with block placing in Terraria style, you can use the Y / Z axis of the world position to calculate the spawn positions of your blocks.
If you do not need Hit data, this would work more than fine in ortho or otherwise. But I reckon you’d need Hits for gameplay reasons. In which case, you’d need to trace anyway. But I’m not sure how 2d/3d this is supposed to be. There’s probably half a dozen ways to handle it.
I’d go with hit data for anything dynamic, like tracing if a character is walking under the cursor or not, but DeprojectScreenToWorld for things that don’t move around, like the building blocks in a grid. Terraria style blocks are basically stored on a grid property from which you can pull what block is under the cursor just by its coordinate. Think of a Map where the Key is the grid Y Z coordinate and the Value is the block.