Use Gamepad right analog stick instead of mouse to control crosshair movement

I’m trying to figure out how I can use the right analog stick of my gamepad to control the movement of my mouse cursor instead of using the mouse. Is this possible? I tried changing the mouse horizontal and vertical axes in the input manager to use joystick axis 3 and 4 but nothing happens. The mouse still works and the analog stick is not recognized.

A little more info if anyone has any suggestions: It’s a 2d game and when the character collects a gun I’m trying to use the right analog stick as a crosshair that you can move anywhere on the screen to shoot. I replace the mouse cursor with a gui texture and just need to get the analog stick to replace the mouse movement so the analog stick position is equal to the mouse position coordinates. Any tips welcome. Thanks!

go to input manager, duplicate ‘mouse x’ and ‘mouse y’ and change the type values and the axis’ on the duplicates, feel free to add a description saying ‘joystick y’ or ‘joystick x’ in the description section.

the reason for this is that the mouselook script only refers to ‘mouse x’ or ‘mouse y’ . Chances are you are only going to use either the mouse or the gamepad so two of the same shouldn’t cause problems.

I was just trying to figure this one out too, using an xbox controller. My method was to create two new axis for the joystick. Make sure you choose which joystick you’re using (for the xbox controller, it’s joystick axis 4 and 5). Gravity/Dead/Sensitivity to 1/.2/1

Since I wanted the option of using the mouse as well, I changed the mouselook script to accept mouse or joystick input. If it’s useful, here’s the script, but you shouldn’t need it if you just want to use the stick. Oh, except you need to go into the script and rename the ‘Mouse X’ and ‘Mouse Y’ to whatever you name your new axis.

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
///   -> A CharacterMotor and a CharacterController component will be automatically added.

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float mousesensitivityX = 15F;
	public float mousesensitivityY = 15F;
	
	public float joysensitivityX = 3F;
	public float joysensitivityY = 3F;

	public float minimumX = -360F;
	public float maximumX = 360F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	float rotationY = 0F;
	


	void Update ()
	{
		
		float Xon = Mathf.Abs (Input.GetAxis ("Joy X"));
		float Yon = Mathf.Abs (Input.GetAxis ("Joy Y"));
			
		if (axes == RotationAxes.MouseXAndY)
		{
			float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousesensitivityX;
			
			rotationY += Input.GetAxis("Mouse Y") * mousesensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
			if (Xon>.05){
				transform.Rotate(0, Input.GetAxis("Joy X") * joysensitivityX, 0);
			}
			transform.Rotate(0, Input.GetAxis("Mouse X") * mousesensitivityX, 0);
		}
		else
		{
			if (Yon>.05){
				rotationY += Input.GetAxis("Joy Y") * joysensitivityY;
			}
				rotationY += Input.GetAxis("Mouse Y") * mousesensitivityY;
				rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
				transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
			
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
}

I eventually got my xbox right-thumbstick to act as a mouse/pointer using a different method.

Edit > project settings > input:
I made 2 new input axis called xboxl + xboxr assigning the 4th and 5th axis and as joystick type.

I then added a new cube (adding a rigid body, turning gravity off) next to my player (player moves with left-thumbstick).

I added this script to the cube

using UnityEngine;
using System.Collections;

public class xboxrightstick : MonoBehaviour {
    public float speed = .5f;
    public float turnSpeed = 60;
    private Rigidbody rig;
    public float distance;
    public Transform target;
    public Transform mouseposition;
    public float maxdist;
	// Use this for initialization
	void Start () {
        rig = GetComponent<Rigidbody>();
        maxdist = 10f;
	}

    void FixedUpdate() {

        Vector3 targetpos = target.transform.position;
    
        mouseposition = rig.transform;
        distance = (Vector2.Distance(transform.position, target.transform.position));
     
        if (Vector2.Distance(transform.position, target.transform.position) <= maxdist)
        {
            float rStickX = Input.GetAxis("xboxl");
            float rStickY = Input.GetAxis("xboxr");

            Vector3 movement = new Vector3(rStickX, rStickY, 0);

            rig.MovePosition(transform.position + movement / speed);
        }
        mouseposition = rig.transform;
    }  
}

Now the cube will not go further than 10f from the player. Now all I have to do is make my player always look at the cube and then make the cube invisible. The player will then appear to be looking in the direction of your right-thumbstick input.

A small thing I have to do is make the cube slowly move towards player other wise it gets stuck when it gets to distance of 10f from player.

Hope this helps someone as the other methods I found online didn’t work for me.
It’s not exactly a mouse but maybe its a step in the right direction for you. Could you simply put this script on an object in the canvas to change the rect transform and then restrict its movement so it cant go off screen? I’m a beginner so i dont know if it would be that easy.