Unable to use Raycasting using xbox 360 gamepad

I’m having trouble trying to get raycasting to work using a gamepad. When I use a mouse it works fine (Input.mouseposition).

This is my code:

using UnityEngine;
using System.Collections;

public class ReticleFocus : MonoBehaviour 
{
    public float distance = 100;
	

	void Update () 
    {
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward *   distance, Color.yellow);

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.gameObject.tag == "US Infantry Squad")
	        {
		        Debug.Log("Ray hit " + hit.transform.gameObject.tag);
	        }
            
        }
	}
}

On the Ray line I’ve tried it several ways but only the Input.mousePosition works:
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
Ray ray = Camera.main.ViewportPointToRay(transform.forward);

If anyone has any ideas let me know, thanks.

Controller input does not work like the Mouse does.

You will need to build some sort of UI cursor that the Xbox controller moves around based on its input axis data in the “screen” space of the camera to then cast a ray on as you are doing in the mouse input.

The mousePosition returns the pixel amount from (0,0) - bottom left to (Screen.width, Screen.height) - top right, which you can then use to cast a ray to the viewport.