ivanshv
ivanshv

Reputation: 47

How to move mouse cursor with joystick?

It seems like this question has already been discussed many times. But I couldn't find the solution for my case. All the discussions are either about camera rotation according to mouse movement or object control via mouse. But I need to control the mouse pointer with joystick (or keyboard). It looks like a really simple question, but I'm confused. Currently, I'm trying to hide hardware cursor and recreat my own, controlling it with Input.GetAxis("Horizontal") or Input.GetAxis("Mouse X") but the cursor just disappears and Event System reports no movement. However, if I use Input.mousePosition.x my custom cursor is well created and controlled with mouse. Unfortunately, I need to control it only with Joystick. In addition, it doesn't work with Project Settings>>Input (or maybe I'm change something wrong there) Thanks in advance.

public class cursor : MonoBehaviour
{
    public Texture2D cursorImage;

    private int cursorWidth = 32;
    private int cursorHeight = 32;
    public float horizontalSpeed = 2.0F;
    public float verticalSpeed = 2.0F;

    void Start()
    {
        Cursor.visible = false;
    }

    void OnGUI()
    {
        float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
        float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
        GUI.DrawTexture(new Rect(h, Screen.height - v, cursorWidth, cursorHeight), cursorImage);
    }
}

Upvotes: 1

Views: 6035

Answers (1)

derHugo
derHugo

Reputation: 90679

You are using

float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
GUI.DrawTexture(new Rect(h, Screen.height - v, cursorWidth, cursorHeight), cursorImage);

h and v will always have very tiny values since multiplied by Time.deltaTime and Input.GetAxis afaik returns usually values between 0 and 1.

These values don't represent an actual cursor position but rather are the change of the position relative to the last frame.

Your cursor will be stuck somewhere in the top-left corner.


Instead you should store the current position and add your values as change to it like

private Vector2 cursorPosition;

private void Start()
{
    Cursor.visible = false;

    // optional place it in the center on start
    cursorPosition = new Vector2(Screen.width/2f, Screen.height/2f);
}

private void OnGUI()
{
    // these are not actual positions but the change between last frame and now
    float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;

    // add the changes to the actual cursor position
    cursorPosition.x += h;
    cursorPosition.y += v;

    GUI.DrawTexture(new Rect(cursorPosition.x, Screen.height - cursorPosition.y, cursorWidth, cursorHeight), cursorImage);
}

Also note that horizontalSpeed and verticalSpeed are in Pixels / Seconds and you probably want some values bigger then 2 ;)

I used 200 and the Up, Down, Left & Right keys.

enter image description here

Upvotes: 2

Related Questions