- Home /
Hardware accelerated mouse
Hi all,
I am trying to create an RTS. Not hiding the standard mouse cursor, I can see that the software update is lagging massively behind the hardware accelerated mouse. A simple example is to create a GUITexture and updating its position every time with new mouse input. You will notice - even with no other program code - that the texture is lagging behind the real mouse movement if you move it rather swiftly. Some people don't seem to notice it, but it drives me crazy. I checked the forums and it seems like a lot of people have the same problem - apparently Unity3D does not support hardware mouse, even though someone wrote a windows plugin for changing the cursor. Note that I need to change more than just the cursor, i.e. the selection box needs to be fluid as well, etc.
I noticed however, that in the AngryBots example the mouse does NOT lag behind the action. I can't find anything special in the code though, so my question really: What does AngryBots do to obtain a hardware accelerated mouse with custom cursors? Also, platform independence is a big thing for me, so I don't just want to tweak the user32.dll or anything similar only to achieve this for windows.
Thank you
Answer by -chris · Oct 16, 2012 at 11:45 PM
Unity 4 is going to have hardware cursor support (available in the Unity 4 beta right now), so if you can wait around for that, don't worry about 3.5 software cursor issues too much.
[http://unity3d.com/unity/beta/notes][1]
- Cursor API supports both software and hardware cursors. - Support for hardware cursors on the following platforms: Windows, Mac OS X, Linux, Flash. - Configure the default cursor in your projects’ Player Settings. [1]: http://unity3d.com/unity/beta/notesNew Cursor API was introduced:
Answer by Eric5h5 · Oct 14, 2012 at 08:17 PM
AngryBots does not have hardware accelerated mouse support. It's not supported in Unity 3 outside of plugins.
Answer by NikEy · Oct 14, 2012 at 10:25 PM
So while hardware acceleration for the mouse is not part of Unity3D (thanks Eric), there seems to be a good way of simulating this to the best effect possible (at least that's the best solution I found after hours of research). Credit should go to this post for giving me the idea: http://forum.unity3d.com/threads/118864-Matching-quot-Fake-quot-Cursor-to-system-mouse-speed-(Suggestion-for-Screen.clampCursor)
In my (short) experience Input.mousePosition is generally good, however, when you are moving the mouse in a fast manner, the software cursor will be noticeably lagging behind the actual hardware cursor. You can see this by setting Screen.showCursor to True.
However, since the effect only happens during rapid movements, one can simply extrapolate from the current movement velocity in order to display the cursor at the point where the user "feels" it is. So in order to perform such a "software acceleration" you can simple lock the mouse and use the mouse delta in order to derive the current mouseposition. By then multiplying the mouse delta with a feasible value (such as 1.5) you artificially created a very smooth mouse acceleration that will sufficiently enough simulate rapid movements that are in line with the user expectation.
Here the code in C#:
using UnityEngine;
using System.Collections;
public class MouseCursor : MonoBehaviour {
public GameObject cursor;
private GameObject _cursor;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
Screen.showCursor = false;
_cursor = (GameObject) Instantiate(cursor);
_cursor.transform.position = new Vector3(0.5f, 0.5f, 0f);
}
// Update is called once per frame
void Update () {
Vector3 mouseDelta = new Vector3( Input.GetAxis("Mouse X") , Input.GetAxis("Mouse Y"), 0f ) * 1.5f;
Vector3 pos = Camera.main.ScreenToViewportPoint(mouseDelta) + _cursor.transform.position;
_cursor.transform.position = pos;
}
}
Forgot to mention that I changed the mouse sensitivity from 0.1 to 0.5 in Edit/project settings/input prior to doing this, so that would be necessary.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Unity 3D Screen.lockCursor Problems 2 Answers
Alt+Tab pointer problem 0 Answers
How to center custom cursor 2 Answers
error CS8025: Parsing error 1 Answer