C# XNA 4.0 Fullscreen Mouse Moves Into Second Monitor - c#

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.

Related

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.

Keep the mouse cursor within the game window

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

Animating an image not playing in game scene

I am trying to make an animation of a countdown.
I first made a Gameobject, created a canvas for it, and inside that canvas I created a UI Image, attached an image to it, and finally created an animation with that image.
Everything is working fine except the animation is not showing in game scene but only in normal scene, but when I go to that UI Image component and start the game I can see the image changing which means that its not the animation but something else.
Does anyone have an idea of what's going on?
There are several possibilities. Depending on if the image is showing up at all, you can check if your UI image is anchored correctly (four triangle marks) or pivot position is where you want it (circle mark). Depending on how this is done, if you Game window resizes in play-mode with the display set to Free Aspect, your image may move off the visible screen or, in some cases, get crushed which usually shows up as a red X in the editor. I can see this happening if your Game window is set to Maximize on Play.

ScrollRect not scrolling when canvas Render Mode is set to Screen Space - Camera

Did you find out why this was happening? I'm having the same issue here.
I had a canvas with a scroll bar working normally, I needed to set the canvas render mode to "Screen Space - Camera", and now the bar doesn't move anymore.
When you change the Canva's Render Mode from Screen Space - Overlay to Screen Space - Camera, an new slot called Render Camera will appear under the Canvas. Drag your main camera into this slot. That camera will be used to send events to the UI elements.
If you've already done this but the problem is still there, delete the ScrollRect and create a new one. Finally, if the ScrollRect is still not moving, make sure that no other UI object is blocking it.
Maybe a hidden panel or image with 0 alpha... Any UI object that is below the ScrollRect in the Hierarchy tab will block the ScrollRect if on top of it.
EDIT:
Once you drag the main camera to the Render Camera slot, Plane Distance slot will appear. Also make sure that it's not set to 0. Mine is set to 100 as shown in the screenshot above and that should work fine.
There might be multiple reasons why your scrollview doesn't work
I encountered with a similar problem. My canvas had a background image so i had to put a Image element without a sprite and set the alpha to "0" within Content of scrollview.
when your viewport field is not referring to viewport object in scroll rect
Try altering the movement type
I also had this problem due to not having an EventSystem in the scene.

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.

Categories