I’m working on a simple mobile input system, with a virtual joystick for movement.
I want to use touch-delta to control camera rotation, but only when the virtual joystick is not being pressed.
However, using the included on screen control examples, this is not the case. The left joystick will move the player, but it will not stop the Primary Touch/Delta [Touchscreen] from also being detected which causes the camera to rotate while trying to use the joystick for movement.
Surely this is a very common use case, is there a simple way to handle it through the new input system? I’m testing in Editor with the included “Player Input” and “Touch Simulation” components.
I figured out a solution, leaving this here incase anyone else runs into the same issue.
I created my own OnScreenControl, using input delta instead of a virtual joystick position. This way it can be fed to whatever input you like (Eg right stick) and work with the system as usual.
It will be blocked by any UI in front of it, as expected. So it can be blocked by a virtual joystick or other UI input. I’ve yet to test it on device, but it works in Editor and build with the TouchSimulation component in the scene somewhere.
To use, just add this to a UI Graphic, (Eg an image with zero alpha, but ensure you tick “Cull Transparent Mesh” on the CanvasRenderer to avoid overdraw) and set the control path through the inspector.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.OnScreen;
public class OnScreenMouse : OnScreenControl, IDragHandler
{
[SerializeField, InputControl(layout = "Vector2")]
private string m_ControlPath;
private bool hasDrag;
private Vector2 delta;
protected override string controlPathInternal { get => m_ControlPath; set => m_ControlPath = value; }
private void LateUpdate()
{
if(hasDrag)
{
SendValueToControl(delta);
hasDrag = false;
}
else
{
SendValueToControl(new Vector2(0, 0));
}
}
void IDragHandler.OnDrag(PointerEventData eventData)
{
delta = eventData.delta;
hasDrag = true;
}
}
5 Likes
Thanks for this . I just don’t understand why then diden’t put this funcion in examples.
1 Like
I tried this but it didnt work, it is not updating hasDrag foe some reason
it works fine on PC but not on device
Hi, I ran into the same problem, tried to use your solution, but when running on the phone, the usual OnScreenControl continues to work and data is read as if from two touch screens, maybe someone has already solved this problem?
Unity should hire this Legend, that touch delta is so annoying