Hi, I have a problem with Input System for mobile in Yandex Games. It works in Editor when I turn on test mobile mode, and works in Web for PC, but doesn’t work on mobile at all.
Here’s my script. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using romanlee17.MirraGames;
public class UnifiedController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[Header("Movement")]
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float runSpeed = 5f;
[SerializeField] private Rigidbody _rigidbody;
[Header("Joystick (Mobile)")]
[SerializeField] private VariableJoystick _joystick;
[Header("Camera (Swipe)")]
[SerializeField] private Transform cameraTransform;
[SerializeField] private float swipeSensitivity = 0.2f;
[SerializeField] private float maxAngleUp = 80f;
[SerializeField] private float maxAngleDown = -80f;
[SerializeField] private Vector3 cameraOffset = new Vector3(0, 1.6f, 0);
[Header("Swipe Panel (UI)")]
[SerializeField] private RectTransform swipePanel;
[Header("Debug")]
[SerializeField] private bool forceMobileMode = false;
private bool isMobile;
private MyPlayer pl;
private ScreenNormal screenNormal;
private float rotationX, rotationY;
private Vector2 swipeStartPos;
private bool isDragging = false;
private int pointerId = -1;
void Awake()
{
isMobile = forceMobileMode || MirraSDK.Device.IsMobile;
if (!isMobile)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
MirraSDK.Device.CursorVisible = false;
MirraSDK.Device.CursorLock = CursorLockMode.Locked;
}
else if (_joystick != null)
{
_joystick.gameObject.SetActive(true);
}
}
void Start()
{
pl = GetComponent<MyPlayer>();
screenNormal = FindObjectOfType<ScreenNormal>();
rotationY = transform.eulerAngles.y;
}
void Update()
{
if (isMobile)
{
HandleSwipeInput();
}
else
{
HandleRotation();
HandleInteraction();
}
}
void FixedUpdate()
{
if (isMobile)
{
HandleMovementMobile();
ApplySwipeRotation();
}
else
{
HandleMovementPC();
}
}
public void OnPointerDown(PointerEventData eventData)
{
if (RectTransformUtility.RectangleContainsScreenPoint(swipePanel, eventData.position))
{
isDragging = true;
swipeStartPos = eventData.position;
pointerId = eventData.pointerId;
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.pointerId == pointerId)
{
isDragging = false;
pointerId = -1;
}
}
private void HandleSwipeInput()
{
if (!isDragging) return;
Vector2 currentPos = Input.mousePosition;
Vector2 delta = currentPos - swipeStartPos;
swipeStartPos = currentPos;
float deltaX = delta.x * swipeSensitivity;
float deltaY = -delta.y * swipeSensitivity;
rotationX = Mathf.Clamp(rotationX + deltaY, maxAngleDown, maxAngleUp);
rotationY += deltaX;
}
private void ApplySwipeRotation()
{
cameraTransform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation = Quaternion.Euler(0, rotationY, 0);
cameraTransform.position = transform.position + cameraOffset;
}
private void HandleMovementMobile()
{
Vector3 moveDirection = new Vector3(_joystick.Horizontal, 0, _joystick.Vertical);
Vector3 localMove = transform.TransformDirection(moveDirection) * moveSpeed;
_rigidbody.linearVelocity = new Vector3(localMove.x, _rigidbody.linearVelocity.y, localMove.z);
_rigidbody.AddForce(localMove * runSpeed * Time.fixedDeltaTime, ForceMode.VelocityChange);
if (pl != null)
{
float mag = moveDirection.magnitude;
if (mag > 0.8f)
pl.animState = 1;
else if (mag > 0.2f)
pl.animState = 4;
else
pl.animState = 0;
}
}
private void HandleMovementPC()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0, v);
Vector3 localMove = transform.TransformDirection(move) * moveSpeed;
_rigidbody.linearVelocity = new Vector3(localMove.x, _rigidbody.linearVelocity.y, localMove.z);
_rigidbody.AddForce(localMove * runSpeed * Time.fixedDeltaTime, ForceMode.VelocityChange);
if (pl != null)
{
float mag = move.magnitude;
if (mag > 0.8f)
pl.animState = 1;
else if (mag > 0.2f)
pl.animState = 4;
else
pl.animState = 0;
}
}
private void HandleRotation()
{
float inputX = Input.GetAxis("Mouse X");
float inputY = Input.GetAxis("Mouse Y");
rotationX -= inputY * swipeSensitivity * 10f;
rotationX = Mathf.Clamp(rotationX, maxAngleDown, maxAngleUp);
cameraTransform.localRotation = Quaternion.Euler(rotationX, 0, 0);
rotationY += inputX * swipeSensitivity * 10f;
transform.rotation = Quaternion.Euler(0, rotationY, 0);
cameraTransform.position = transform.position + cameraOffset;
}
private void HandleInteraction()
{
if (screenNormal == null) return;
if (Input.GetKeyDown(KeyCode.F)) screenNormal.OnReportPressed();
if (Input.GetMouseButtonDown(0))
{
screenNormal.OnKillPressed();
screenNormal.OnUsePressed();
}
if (Input.GetMouseButtonDown(1)) screenNormal.OnMapToggle();
if (Input.GetKeyDown(KeyCode.Escape)) screenNormal.SetPause();
}
}