Hey.
I am trying to make a mobile game where you move a ball using a joystick and make it jump using a button. Both the joystick and the button work, but not simultaneously. I checked online to find a solution for multi-touch input and got as answers using a for loop or a foreach loop which would handle any touch in the array, however, it didn’t work in my case. I use OnDrag(), OnPointerDown() and OnPointerUp() for the joystick as well as OnClick() for the button. I hope someone can help me.
The scripts I shared are quite long, please bear with me.
Here is the script handling the joystick:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
Vector2 pos;
private Image back; //back is a circle used as the background of the joystick
private Image fore; //fore is a child of back, it is a small circle on which you would drag your finger to make the ball move
public Transform ball;
public int force = 2;
public GameObject button;
public Touch t;
public void Update()
{
Debug.Log( "Joysick fingerId = " + t.fingerId );
}
private void Start()
{
back = GetComponent<Image>();
fore = transform.GetChild( 0 ).GetComponent<Image>();
}
public virtual void OnDrag( PointerEventData ped )
{
if ( RectTransformUtility.ScreenPointToLocalPointInRectangle( back.rectTransform, ped.position,
ped.pressEventCamera, out pos ) )
{
pos.x = ( pos.x / back.rectTransform.sizeDelta.x ) * 2;
pos.y = ( pos.y / back.rectTransform.sizeDelta.y ) * 2;
pos = ( pos.magnitude > 1 ) ? pos.normalized : pos;
fore.rectTransform.anchoredPosition = new Vector3( pos.x * ( back.rectTransform.sizeDelta.x / 2),
pos.y * ( back.rectTransform.sizeDelta.y / 2 ) );
}
}
public virtual void OnPointerDown( PointerEventData ped )
{
OnDrag( ped );
}
public virtual void OnPointerUp( PointerEventData ped )
{
pos = Vector2.zero;
fore.rectTransform.anchoredPosition = Vector2.zero;
}
public void FixedUpdate()
{
Vector2 input = pos;
ball.GetComponent<Rigidbody>().AddForce( input.x * force, 0, input.y * force );
}
public float Horizontal()
{
if ( pos.x != 0 )
{
return pos.x;
}
else
{
return Input.GetAxis( "Horizontal" );
}
}
public float Vertical()
{
if ( pos.y != 0 )
{
return pos.y;
}
else
{
return Input.GetAxis( "Vertical" );
}
}
}
And here is the script for the button:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class Button : MonoBehaviour
{
public Transform ball;
public int force = 89;
public int jumpCount = 0;
public GameObject joystick;
public Touch t;
public void JumpF()
{
//for and foreach loops didn't work
/*foreach ( Touch t in Input.touches )
{
if ( jumpCount < 2 )
{
ball.GetComponent<Rigidbody>().AddForce( Vector3.up * force );
jumpCount += 1;
Debug.Log( t.fingerId );
Debug.Log( t.tapCount );
}
}*/
for ( int i = 0; i <= Input.touchCount; i++ )
{
Debug.Log( "Jump" ); //I removed the jump functionality and replaced it with a Debug.Log() function for debugging purposes
joystick.GetComponent<Joystick>().StopAllCoroutines();
}
}
public void Update()
{
Debug.Log( " Button fingerId = " + t.fingerId );
}
}
Thank you for your attention.