Hello,
I’m a new Unity developer and working on a third-person controller for mobile development (Android). So, I’ve added joysticks (fixed joysticks from the Unity Asset Store). The problem is that when I try to move the player with the joysticks on mobile, the touch screen works with the joysticks, and also when I click on the screen, the player is shooting (the shooting function is working unintentionally). I don’t know why the touch screen and joysticks aren’t separated. I didn’t make any script for the touch screen but it’s working automatically after mobile build because the project has the Input System package. I found a script in Input System package called “UiVirtualTouchZone.cs”. So, I’ve modified it as provided below. I have added “!EventSystem.current.IsPointerOverGameObject(eventData.pointerId)” but it still doesn’t work. It’s like touch events taking the whole screen and working abnormally. Please help me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class UIVirtualTouchZone : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
[Header("Rect References")]
public RectTransform containerRect;
public RectTransform handleRect;
[Header("Settings")]
public bool clampToMagnitude;
public float magnitudeMultiplier = 1f;
public bool invertXOutputValue;
public bool invertYOutputValue;
//Stored Pointer Values
private Vector2 pointerDownPosition;
private Vector2 currentPointerPosition;
[Header("Output")]
public UnityEvent<Vector2> touchZoneOutputEvent;
void Start()
{
SetupHandle();
}
private void SetupHandle()
{
if(handleRect)
{
SetObjectActiveState(handleRect.gameObject, false);
}
}
public void OnPointerDown(PointerEventData eventData)
{
if (!EventSystem.current.IsPointerOverGameObject(eventData.pointerId))
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, eventData.position, eventData.pressEventCamera, out pointerDownPosition);
if (handleRect)
{
SetObjectActiveState(handleRect.gameObject, true);
UpdateHandleRectPosition(pointerDownPosition);
}
}
}
public void OnDrag(PointerEventData eventData)
{
if (!EventSystem.current.IsPointerOverGameObject(eventData.pointerId))
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, eventData.position, eventData.pressEventCamera, out currentPointerPosition);
Vector2 positionDelta = GetDeltaBetweenPositions(pointerDownPosition, currentPointerPosition);
Vector2 clampedPosition = ClampValuesToMagnitude(positionDelta);
Vector2 outputPosition = ApplyInversionFilter(clampedPosition);
OutputPointerEventValue(outputPosition * magnitudeMultiplier);
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (!EventSystem.current.IsPointerOverGameObject(eventData.pointerId))
{
pointerDownPosition = Vector2.zero;
currentPointerPosition = Vector2.zero;
OutputPointerEventValue(Vector2.zero);
if (handleRect)
{
SetObjectActiveState(handleRect.gameObject, false);
UpdateHandleRectPosition(Vector2.zero);
}
}
}
void OutputPointerEventValue(Vector2 pointerPosition)
{
touchZoneOutputEvent.Invoke(pointerPosition);
}
void UpdateHandleRectPosition(Vector2 newPosition)
{
handleRect.anchoredPosition = newPosition;
}
void SetObjectActiveState(GameObject targetObject, bool newState)
{
targetObject.SetActive(newState);
}
Vector2 GetDeltaBetweenPositions(Vector2 firstPosition, Vector2 secondPosition)
{
return secondPosition - firstPosition;
}
Vector2 ClampValuesToMagnitude(Vector2 position)
{
return Vector2.ClampMagnitude(position, 1);
}
Vector2 ApplyInversionFilter(Vector2 position)
{
if(invertXOutputValue)
{
position.x = InvertValue(position.x);
}
if(invertYOutputValue)
{
position.y = InvertValue(position.y);
}
return position;
}
float InvertValue(float value)
{
return -value;
}
}