Try to detect if player's touch is inside the play area, and if so, start a timer. I already have the starting a timer part working for keyboard input, just not touch (mobile) input.
if(...) {
timer.Start();
}
My game board is a Rect Transform with a sprite:
EDIT:
I tried what's in the duplicate but it still didn't work.
public class PlayerController : MonoBehaviour, IBeginDragHandler
{
void Start()
{
...
// For touch controls
addPhysics2DRaycaster();
}
// For touch controls
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
}
Here is where I try to start the timer:
public void OnBeginDrag(PointerEventData eventData)
{
timer.Start();
}
Here is the code for the controls:
// FixedUpdate() - called before physics calculations
void FixedUpdate() {
// Touch swipe input
if(controlToggle.isOn) {
joystick.SetActive(false);
controlToggle.GetComponentInChildren<Text>().text = "Swipe";
if(Input.touchCount > 0) {
Touch myTouch = Input.touches[0];
if(myTouch.phase == TouchPhase.Began) {
touchOrigin = myTouch.position;
} else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
float moveHorizontal = x;
float moveVertical = y;
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed);
}
}
}
...
}