How can I control cursor with joystick
Asked Answered
M

5

0

Hello people,
I am doing a custom cursor that needs to be controlled by the joystick. To control the cursor with the mouse is quite easy, but I am trying to figure the equivalent but with the joystick axis input.
To move cursor with mouse is done like this:

function OnGUI ()
{
    GUI.DrawTexture( Rect(Input.mousePosition.x-cursorSizeX/2, (Screen.height-Input.mou.y)-cursorSizeY/2, cursorSizeX, cursorSizeY), myCursor );
}

How can I do it with Joystick?

Meatiness answered 12/2 at 21:3 Comment(1)

while it's not EXACTLY what you're looking for, I'm going through something similar, and have made some progress. (not complete, by any means, but maybe it'll help you) https://mcmap.net/q/26971/unity-trying-to-highlight-text-with-controller

Virago
P
0

Inside of the Input-Manager, you can set the joystick axis to the mouseX and mouseY. That way, you should be able to control the cursor using Input.GetAxis(Mouse X) and Input.GetAxis(Mouse Y).

Input Manager is located under: EditProject SettingsInput

and here is a mapping guide for controllers:
Joystick/Controller - Unity3D (archive.org)

Prelate answered 12/2 at 21:10 Comment(0)
M
0

Already figured it out:

x = Input.GetAxis("Horizontal") * speed * Time.deltaTime; 
y = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Meatiness answered 12/2 at 21:10 Comment(1)

can you tell me exactly how this makes your mouse move with your controller? Explanations of answers would be really helpful.

Slain
H
0

Hi there ! :slight_smile:

I resurrect this subject after a few years because I have trouble controlling the Unity Cursor with the Xbox controller left joystick. The context in game is this one :

  • Oculus Player with movements controlled by left Xbox joystick
  • Pressing B Button, a Pause Menu is displaying (simple canva Camera Space)
  • Unity Cursor appear and shall “OnClick” buttons (among them : resume)

Of course, my question is about the third step of this process as I fail to recover joystick movement while canceling the one of the first step’s setup. The cursor in pause menu shall be controlled by left joystick.

I’m pretty new to c sharp and i’m confused between both coding and unity’s drag & droping logic on this specific task.

I will surely appreciate any help from you ! :heart:

EDIT:

Now struggling on how to constraint GUI Cursor position at screen limits and how to make the GUI cursor act like a mouse, regarding “OnClick” behavior.

using System.Collections.Generic;
using UnityEngine;

public class XboxCursor : MonoBehaviour
{
    public Vector3 originalPosition; //Original Position stored for "Start at Center position" Cursor behavior
    private Vector3 startPos;
    private Transform thisTransform;
    public float sensitivity = 0.2f;
  
    void Start()
    {
        thisTransform = transform;
        this.transform.position = originalPosition; //Wrong method for reset to center position...
    }
  
    void Update()
    {
        // Joystick Axis Acting
        Vector3 inputDirection = Vector3.zero * sensitivity;
        inputDirection.x = Input.GetAxisRaw("Horizontal") * 0.1f;
        inputDirection.y = Input.GetAxisRaw("Vertical") * 0.1f;
        thisTransform.position = startPos + inputDirection * sensitivity;
        startPos = thisTransform.position;
  
  
        //Screen Constraint  -- Not Working at all for some reason
        this.transform.position = new Vector3(Mathf.Clamp(transform.position.x, -1f, 1f),
                        Mathf.Clamp(transform.position.y, -2f, 2f), transform.position.z);
        
        // Mouse OnClick Mimetism
        if (Input.GetButtonDown("Button A"))
        {
            Input.GetMouseButtonDown(0);
            Debug.Log("Mouse Click !"); //But still at real mouse position ! xP
            
           //TP Mouse (Bad Opt)
           //Raycast GUI (Good Opt... Searching Doc...)
        }
    }
}

EDIT : Leaving this here, but subject abandoned after new UX project approach.

Hyperform answered 12/2 at 21:13 Comment(0)
P
0

I want to know about this… how to make virtual mouse cursor for control via Nintendo Switch pro controller connected to PC.

Primavera answered 27/1, 2021 at 7:29 Comment(0)
P
0

There is also a paid solution in the unity asset store that does this for Windows:
https://assetstore.unity.com/packages/tools/input-management/set-cursor-position-mouse-clicks-api-17177?aid=1011lkZiC

Publishing answered 12/2 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.