Keep the mouse cursor within the game window - c#

How can I make it so that the mouse cursor within the game window so that the cursor won't, for example, go into the other screen if you're using dual screens? I do not want to use the Screen.lockCursor since I want to be able to use the mousepointer inside the game and not make it hidden and centered which you do with the lockCursor function.

This might be able to help you Cursor.lockState = CursorLockMode.Confined;
For more information look at the API at https://docs.unity3d.com/ScriptReference/Cursor-lockState.html

Related

ClipCursor not working for touch based experience

I am developing a WPF application, where I need to allow user to rearrange objects by drag-drop. It is basically dragging objects within a canvas area and moving them to another area of the canvas. The mouse cursor would change to a drag cursor while dragging is happening.
The requirement is like the mouse pointer cannot leave the canvas area while the dragging is in progress. For that I am using the Win32 ClipCursor api to restrict the cursor to a certain boundary of the screen.
This is working fine when mouse is used. However, if touch is used, i.e. if the pointer is moved through touch, it is possible to take the cursor out of the bounding rectangle. Can anyone please help to understand why it is happening? I am handling the PreviewMouseMove and MouseLeftButtonUp events for starting and ending the drag. Do I need to handle the Touch and the Stylus events as well?
Thanks!

Unity 2D - Detect Tiles with Mouse

Overview: I am making a 2D Tower Defense game with using Tilemaps as the environment and sprites as the towers and enemies. The mouse needs
Problem: I want to trigger different events/methods depending on which tile or sprite the mouse is hovering or clicking on.
Example - Hovering over buildable tiles shows a highlighted tile, but the highlight disappears when the mouse is over a "dead" un-buildable tile.
After 10+ hours of research, I think I need to use Raycasts, 2D Colliders, and Layers to detect when the mouse hovers over or clicks on a tile/object, but I don't know how to trigger different events/methods depending on which specific layer or tag the mouse is interacting with.
Question: How do I detect and access a tile or gameobject with a mouse hover/click? and is there a way to trigger different methods depending on tags/layers I assign to the things I want to detect on hover/click?
First of all you don't need raycasts or colliders to detect mouse events. There are many ways to do this, but you can create eventListener for use on multiple objects. Also as a simple solution you can consider using scripts(includes UnityEventListeners) attached to the objects you want to control.

XNA Mouse position is off

I'm working on a miniature golf game in XNA, I originally had everything in Game.cs (main), but I now want it to be more object-oriented, so I made separate class for most of my stuff.
When I had everything in Game.cs, it was working fine, now it doesn't.
What is happening is this:
When my cursor is at the top left corner of the game window, it's like X=200, Y=50.
It's supposed to be X=0, Y=0.
Even when I look for the 0, 0 position, it's way outside the game window.
Does anyone know what could be causing this?
How is that possible? Your cursor position is the mouse position. Simply draw the cursor there.
Unless you are talking about the Windows cursor. In that case, yes, the input data in XNA will not match the Windows cursor movement. They probably apply some modifiers for acceleration, etc. You have to interprete the input data yourself. In other words, draw your own cursor.

C# XNA 4.0 Fullscreen Mouse Moves Into Second Monitor

The heading just about says it all. I derive from the XNA Game class, and set the IsMouseVisible = true. I have a dual screen setup with the secondary display being extended from the primary. I set my game to fullscreen mode (GraphicsDeviceManager.IsFullScreen = true). All works fine and fullscreen mode is initialized. The only problem I have is that the mouse does not stay bound within the fullscreen game. As soon as it reaches the edge of the fullscreen game, it seamlessly moves over onto the secondary monitor. Is this expected bahaviour?
I do a Mouse.GetState() to retrieve mouse info every frame on the same thread in which my game runs.
I have even tried setting Mouse.WindowHandle = GameWindowHandle before every call to GetState, but it does not change anything.
Any ideas? Or is this expected behaviour and I need to clamp the mouse to the fullscreen area myself?
Regards,
Riaan
Yes, exactly, clamp your mouse to the screen that you want it to stay on. This is similar to an approach that I (and many others) use when I implement sprite cursors for my games. I hide the system mouse, and clamp it to the center of the screen so that it doesn't show up on other monitors.
The only downside is that you have to un-clamp your mouse if you minimize the game, etc. Otherwise it stays stuck, which can be really annoying.

How to limit cursor movement in WPF based app?

I'm working with a WPF app, more specifically a Canvas with draggable elements.
Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.
The event which can start a drag is shown below
private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
... logic to figure out if this is a valid drag, blah blah blah ...
this.IsDragging = true;
// TODO: clip the available cursor movement to actual width of Canvas
}
On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.
I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.
Anyone have a better way to limit the cursor boundaries?
There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.
Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.
If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.
Good luck!
You should be able to do it using the ClipCursor native API.

Categories