- Home /
How can I control cursor with joystick
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?
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)
http://answers.unity3d.com/questions/543403/taking-steps-to-all-gamepad-control.html
Answer by Visual Programmer · Sep 26, 2013 at 07:03 PM
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: Edit-->Project Settings-->Input
and here is a mapping guide for controllers.
Answer by Palanysamy · Sep 27, 2013 at 06:44 AM
Already figured it out: x = Input.GetAxis("Horizontal") speed Time.deltaTime; y = Input.GetAxis("Vertical") speed Time.deltaTime;
can you tell me exactly how this makes your mouse move with your controller? Explanations of answers would be really helpful.
Answer by MushayDroom · Jan 18, 2019 at 08:54 AM
Hi there ! :)
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 ! ♥
EDIT :
Now struggeling 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 abandonned after new UX project approach.
Answer by leegod · Jan 27, 2021 at 07:29 AM
I want to know about this... how to make virtual mouse cursor for control via Nintendo Switch pro controller connected to PC.
Your answer
Follow this Question
Related Questions
Get DPad input value via GetButton instead of GetAxis? 8 Answers
Add Cursor (Top-Down-Shooter) 1 Answer
How to get input from wheel(car direction) joystick? 1 Answer
Is there a way to create a floating JoyStick using the On-Screen Stick component? 0 Answers
Joystick Zone + Screen-swipe touch input clash. Solution??? 1 Answer