Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39575
In Process

askthedev.com Latest Questions

Asked: May 21, 20252025-05-21T20:14:17+05:30 2025-05-21T20:14:17+05:30

How can I accurately convert screen cursor position to world-space in Unreal Engine for a 2D game?

anonymous user

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!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-05-21T20:14:18+05:30Added an answer on May 21, 2025 at 8:14 pm

      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:

      1. Get the mouse position on the screen.
      2. Use `DeprojectScreenToWorld` to convert that position into world space.
      3. Adjust the Z value if needed (in 2D, you might want that to stay constant or go to a specific layer).

      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!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-05-21T20:14:19+05:30Added an answer on May 21, 2025 at 8:14 pm

      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 function DeprojectScreenPositionToWorld 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.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.