Hello, i have an issue with a OnDrag function for my virtual joystick which i am trying to use to move the camera on x, y axis. I have the script below, which works good when played in game mode with mouse. But on my mobile the drag function does not work, meaning the joystick handler stays put. The funny thing is that on pointer down function, which calls the same ondrag(), it works good on mobile… Can some of you help me figure ouy what am I missing here?
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoystickMovement : MonoBehaviour, IDragHandler,IPointerUpHandler,IPointerDownHandler
{
private Image BgImage;
private Image joystickImg;
Vector3 InputVector;
Vector3 ZeroPos;
float distanceLeft, distanceRight;
Transform Cam;
public float speed, speedBack, speedFront;
private void Start()
{
Cam = Camera.main.transform;
ZeroPos = Cam.transform.position;
BgImage = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(BgImage.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / BgImage.rectTransform.sizeDelta.x);
pos.y = (pos.y / BgImage.rectTransform.sizeDelta.y);
InputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
InputVector = (InputVector.magnitude > 1.0f) ? InputVector.normalized : InputVector;
joystickImg.rectTransform.anchoredPosition = new Vector3(InputVector.x * (BgImage.rectTransform.sizeDelta.x / 3), InputVector.z * (BgImage.rectTransform.sizeDelta.y / 3));
}
}
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
InputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
I can provide more information if needed.