Converting mouse coords to world coords to accurately place objects

I’m making a 2D game similar to Terraria or Minecraft (namely, placing blocks according to a grid). I’ve looked up how to convert a 2D mouse cursor position to a 3D environment using a line trace. However, now in my test project, I have another issue.

Screenshot:

When I click, I’ve set it to mark where I clicked according to the line trace (which is correct every time and shown by the red squares) and place a new block (the grass squares shown in the screenshot) there. (They aren’t aligned to a grid currently.) This works alright near the center of the screen, but not near the edges.

How do I correct this so the blocks are placed exactly where my mouse points, even near the edge of the screen? I’m using a Perspective (non-Orthographic) camera.

For reference, this is my blueprint:

It doesn’t work because the “break hit result” node needs a hit to occur. Use a trigonometric approach instead:

I copied your blueprint into my game:

But now it places a block right in front of the camera, and ignores left/right axis (X in my game), instead placing it at the center of the view. In the image below, the red box is approximately where I clicked (photoshopped in).

Try switching x’s and y’s in the blueprint. In my version (the side scroller game example) the player was moving left and right on the y axis.

Ok, I swapped the x’s and y’s, and it’s closer to my desired functionality. However, the blocks are being placed too close to the center, i.e. I click at the top right corner and a block is placed above and to the right of center, but not where I clicked.

Well, based on your description of what you want, there’s a much simpler way to do this…

Here ya go:

http://i.imgur.com/zkZTrWL.png

That works for placing the block at the Y of my background (which is y-440), but I’m trying to place it at y0, on the plane where my character moves. When I break the vector and replace y with 0, I have the same problem as I originally had.

I gave an answer based on what you wrote. But it makes sense haha, I don’t know why I didn’t consider it. I also see it in the BP you posted, missed it I guess.

But, based on what you described this time, which is wanting to place something at your mouse location, but on a fixed y coord, then it’s also very simple, and it sounds like you already tried it:

http://i.imgur.com/bP4EjvO.png

Is this what you tried? It works fine for me.

Is this what you’re trying to accomplish? -

(This comment got messed up, it’s out of place… I dunno.)

Yes, my blueprint looks like yours (although my Get Hit Result Under Cursor by Channel returns a Hit Result, so I have to break it). I should note, I’m using a perspective camera instead of an orthographic camera that comes with the 2D Sidescroller example, since I want to incorporate parallax scrolling in my game.

You can right click on the hit result and “split struct pin”.

So, yeah, changing the camera broke it. But a simple solution I did to fix it was to add a collision plane at the correct y coord that is only triggered by the mouse. That should give the results you’re looking for.

By the way, is parallax scrolling when the background and foreground objects move to make it look like a 3d world instead of 2d?

That looks very cool

That’s right, parallax scrolling is where different aspects of the background move at different paces (closer objects move faster).

How would I go about adding a collision plane only triggered by the mouse? Would that just be a blocking volume with the UI collision preset?

I just made an actor class, added a single box collision component, made it into a plane, make it as large as the world plane, and set it’s collision properties to only be affected by the mouse.

I then placed then plane on the same y as the character. Done!

Looks like that did it. Upvoted and accepted as answer. Thanks for all your help!