I will be honest, I feel like in Unity you can easily Lock, and Unlock your cursor but I really struggled for a while with this issue inside of Unreal Engine 5. I looked all over the web using search terms like “UE5 Lock Mouse to Viewport Issue”, “UE5 Show Mouse Issue” and of course switching out the term “issue” with “problem”. I did find a lot of posts out there but most of them only solved for one part of the problem. I believe I have figured out solutions to the issues I was running into. I even made a video talking about the solutions, but I also have gone ahead and written this article to help those who just need a refence point or for those who have watched the video and want stills of the Blueprints.
Fill free to watch the video on YouTube or stay on this page watch the embedded video.
If you don’t want to watch the video you want to have visual reference, I have broken down the blueprint below.
Here is the whole function end to end that I placed in the level blueprint.
First, we have a trigger event you can have assigned to a key or button on a gamepad.
Next, we have a flip flop which will do first trigger the “A” path the first time that trigger event is fired and then the next time it is fired it will go down the “B” path. You will notice in both paths and all functions in these paths we are always targeting the same player controller.
Using SET we can show or hide the mouse. Since when the game runs our mouse is already hidden, we know that we want the first time someone triggers the event for it to show the mouse.
Next, you will notice that for path “A” we are setting the input mode to both game and UI. The reason for this is so that the mouse can be listened for on both UI objects and the game world objects.
Lastly, we have the look input function either ignore the mouse data or reset.