How do I set mouse boundaries?

Well in C++,
You do like this…

if (cursorlocation.Y < cameralocation.Y +1000.0f && cursorlocation.Y > cameralocation.Y - 1000.0f && cursorlocation.X < cameralocation.X +1000.0f && cursorlocation.X > cameralocation.X - 1000.0f )

{
movecursor();
}

I am doing a top down game and the character is following the cursor around the screen. The control handling isn’t perfect, if the cursor touches one of the 4 walls (in the box room) the character will stop moving completely until the cursor is moved back to the floor…

What would be the easiest solution to this problem? I am fairly new with UE4 but I have tried adding a trigger box but I can not think of any nodes which can basically tell the cursor to stay within the box at all times.

I will leave a screenshot of the defualt cursor blueprints that come with the top down game template, I have only one gate as I did not require it for what I am doing but the rest of the nodes are all from the template.

Any advice is greatly appreciated.

Many thanks.

I’m gonna see if I can figure out how to translate this in BP. The main nodes are probably gonna be Branch - get Cursor location and equal to. I’ll try to set up something.

The real trick here is that there currently isn’t a way to set the mouse cursor position from Blueprint built into UE4.

In general, the process is fairly simple:

  • Determine the X and Y limits for your mouse cursor; you’ll need a minimum and maximum value for each. If you have a static camera, you can set values according to the room you’re in, or if they’re all the same size (like the original Zelda, or Binding of Isaac), you can use the same values all the time. If your camera moves, you’re going to have to update the values based on that movement.
  • Determine the current position of the mouse. This one is easy, you can do that by getting a reference to the player controller, and calling GetMousePosition(), you’ll get two floats - one for X and one for Y. Bear in mind that mouse coordinates are expressed in screen space, as X and Y coordinates on your screen, not within the game world. You can use DeprojectMousePositionToWorld from the PlayerController to get the mouse position in world coordinates, or you can convert your room position limits to screen coordinates.
  • Compare the current mouse position to your limits. Set branches for if X is greater than MaxX or less than MinX, and if Y is greater than MaxY or less then MinY.
  • If your mouse position is outside the limits, set the mouse position to be within the limites. (i.e., if X is greater than MaxX, set the X value to MaxX)

The last step is the real trick here. Within C++ you can get a reference to the Viewport from your player controller, and it has a method called “SetMouse”; but it’s not currently exposed to BP. You can either edit the engine to expose this method, or otherwise write your own object to give you this method. Or you can install Rama’s BP Library plugin that gives you the SetMouse functionality.

(39) Rama's Extra Blueprint Nodes for You as a Plugin, No C++ Required! - Blueprint - Unreal Engine Forums!

I think I’m going to go through the native SetMouse method and see if there’s any reason Epic hasn’t exposed it to BP, and if not I’ll just do it myself and see if they accept the modification.

This could at least limit the movement of the green location in world space, but to limit your mouse you could only limit it on your screen and if I remember right you wanted to move the camera around. So the player could just move to the right to get outside the world with the mouse, but in this way you can limit the green thing to the bounds you want. You may have to change the lower variable to a different one, but not a simple =. This would cause it to stop only if it touches exactly the limit line, not over or under it.

I’ve editted it and it should work now by not getting the input if it is outside the boundaries on the world, but not on screen.