0
\$\begingroup\$

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?

\$\endgroup\$
0
\$\begingroup\$

Here is how I fixed this problem. I disabled my touch scripts and rebuilt and I was still getting touch movements so then I figured that every script I've tried work just fine. Cinemachine's free look camera naturally accepts inputs from both touch and mouse for it's ability to look around. So my code only checks if a touch is over a UI object before allowing touch movements from my script and not from Cinemachine's.

How I fixed this is:

private float InitSpeedX, InitSpeedY;
public CinemachineFreeLook cFL;

void Update ()
{
      if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(Index).fingerId))
      {
             cFL.m_YAxis.m_MaxSpeed = 0;
             cFL.m_XAxis.m_MaxSpeed = 0;
      }
      else
      {
             cFL.m_YAxis.m_MaxSpeed = InitSpeedX;
             cFL.m_XAxis.m_MaxSpeed = InitSpeedY;
      }
}

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.