I know this question has been asked a lot of times, and I have tried the solutions that were provided and I still have no valuable result, so I thought...maybe a new set of eyes could help.
I have a game in which the player can navigate around the scene environment using a virtual joystick and look around the scene by scrolling through the screen(like it is seen in most mobile fps games).
The problem that I am having now is that my "look around" script allows touch inputs to pass through UI elements on mobile, but everything works fine on the editor.
Useful tips:
1) I am using Unity 2019.3.0f5
2) I setup the script to work with Cinemachine Free Look, and my script(seen below) is attached to it.
Here is my code:
using UnityEngine.EventSystems;
using UnityEngine;
using Cinemachine;
public class CinemachineCoreInputTouchAxis : MonoBehaviour
{
private readonly float TouchSensitivity_x = 10f;
private readonly float TouchSensitivity_y = 10f;
[SerializeField] private bool UseTouchControls = true;
private static int Index = 0;
void Start()
{
if(UseTouchControls)
CinemachineCore.GetInputAxis = HandleAxisInputDelegate;
}
private float HandleAxisInputDelegate(string axisName)
{
switch (axisName)
{
case "Mouse X":
if (Input.touchCount > 0 && !IsPointerOverUIObject())
return Input.touches[Index].deltaPosition.x / TouchSensitivity_x;
else
return Input.GetAxis(axisName);
case "Mouse Y":
if (Input.touchCount > 0 && !IsPointerOverUIObject())
return Input.touches[Index].deltaPosition.y / TouchSensitivity_y;
else
return Input.GetAxis(axisName);
default:
Debug.LogError("Input <" + axisName + "> not recognized.", this);
break;
}
return 0f;
}
public static bool IsPointerOverUIObject() //Called to check if the pointer is over a ui object
{
bool value = false;
for (int i = 0; i < Input.touches.Length; i++)
{
Index = i;
value = EventSystem.current.IsPointerOverGameObject(Input.GetTouch(Index).fingerId);
if (value)
break;
}
return value;
}
}
Can anyone please let me know what I am failing to see and how to fix it?