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.
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.
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).
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.
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:
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?
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!