I’m working on a 2D game in Unreal Engine and I’m stuck trying to convert the screen cursor position to world space accurately. So, here’s the situation: I’ve got my character positioned at X=-500 and Z=700, but my main challenge revolves around how the cursor’s position relates to the world.
I’ve been using the built-in blueprint function `ConvertMouseToWorldSpace`, but it’s left me scratching my head. When I use this function to translate the cursor position—which is hovering over my character—I get this weird result where the “world” location is just a fraction of a float number that seems to be directly influenced by the camera’s location. It feels like the conversion isn’t taking the full camera matrix into account, which would make sense if I’m aiming for accuracy.
The way I see it, I need to figure out how to determine where my character should appear in world space that aligns visually with the cursor’s position on the screen. I thought about the option of projecting the actor’s location to screen coordinates, and then doing some math between the projected screen location and the cursor’s location. That approach technically works, but it’s really cumbersome and feels like a hack rather than a clean solution.
Does anyone have a better way to handle this transformation? I want to retrieve the appropriate world location of the cursor without having to resort to this clunky method. Is there a more effective way to account for the camera projection when converting mouse coordinates to world space, particularly for a 2D game setup? Any tips or advice from your own experiences would be super helpful!
In Unreal Engine, using
ConvertMouseToWorldSpace
alone typically yields just a direction vector and a location offset influenced heavily by your camera’s position. For a precise 2D setup, you’ll achieve better results by manually performing a line-plane intersection, treating your gameplay area as a fixed plane (such as Z = 0, Y = 0, or whichever plane your game uses). You can utilize the functionDeprojectScreenPositionToWorld
from your PlayerController. It will output both the world location and direction vector for your mouse cursor. Once obtained, you can perform a simple line-plane intersection calculation, projecting from your camera along the direction vector until intersecting the plane that your actors exist on.Specifically, grab your PlayerController and call
DeprojectScreenPositionToWorld
, taking in your mouse coordinates and retrieving the resulting world location and direction. Assuming a known 2D plane or axis-aligned plane (e.g., the XZ-plane at Y=0 for a side-view setup), you can solve the intersection mathematically in blueprint or C++. This method is accurate, clean, and accounts for the camera’s full projection matrix, removing any ambiguity or dependency on actor screen projection hacks.Sounds like you’re hitting a pretty common snag with converting mouse coordinates to world space in Unreal Engine, especially for a 2D game! The `ConvertMouseToWorldSpace` function should usually do the trick, but if it’s giving you weird values, it might be worth checking a few things.
First off, make sure your camera settings are consistent and that you’re actually looking at the right area of the world. Sometimes, if the camera is off or has weird scaling, it messes up the calculations. If your character is positioned at X=-500 and Z=700, that’s like halfway up in the world; just double-checking where your camera is pointing can help.
If the `ConvertMouseToWorldSpace` isn’t cutting it, you could try using a combination of other functions. One trick is to use `DeprojectScreenToWorld`, which can be more reliable for what you’re trying to achieve. This function takes the screen coordinates (like your mouse cursor position) and transforms them into a world location, accounting for the camera’s view and projection.
Here’s a step-by-step idea:
This method should give you a more accurate point in world space that corresponds with your cursor. Just remember that in 2D, you might not need that full 3D space handling, so you can refine the Z value when you get your world location.
Give that a shot! And don’t hesitate to tweak your camera settings if things still feel off. It can be a bit of a puzzle figuring out how all these pieces fit together, but you’ll get there!