Тап мешает свайпу
Есть джойстик, по которому происходит движение влево-вправо и по свайпу предусмотрен прыжок. По отдельности все работает, но невозможно и бежать, и прыгать одновременно. Вообще нет идей, как исправить это, прошу помощи)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Player : MonoBehaviour
{
[SerializeField] private float speed = 3f;
[SerializeField] private int maxHealth = 5;
[SerializeField] private float jumpForce = 15f;
public int currentHealth;
private bool stor = true;
private bool isGrounded = false;
private bool isFacingRight = true;
private bool isSwiping;
private float groundRadius = 0.2f;
private float ddDelta = 80;
public LayerMask whatIsGround;
public Transform groundCheck;
public Rigidbody2D rb;
private SpriteRenderer sprite;
private Animator animator;
public Joystick joystick;
public static Player Instance { get; set; }
public static event OnSwipeInput SwipeEvent;
public delegate void OnSwipeInput(Vector2 direction);
private Vector2 impulser = new Vector2(-6, 2);
private Vector2 impulsel = new Vector2(6, 2);
private Vector2 tapPosition;
private Vector2 swipeDelta;
private void Start()
{
Player.SwipeEvent += Jump;
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
Instance = this;
}
private void FixedUpdate()
{
CheckGround();
if (joystick.Horizontal > 0 && !isFacingRight) Flip();
else if (joystick.Horizontal < 0 && isFacingRight) Flip();
}
public void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase==TouchPhase.Began)
{
isSwiping = true;
tapPosition = Input.GetTouch(0).position;
}
else if (Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(0).phase == TouchPhase.Ended)
{
ResetSwipe();
}
}
CheckSwipe();
if (joystick.Horizontal != 0) Run();
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
stor = !stor;
}
private void Run()
{
;
Vector3 dir = transform.right * joystick.Horizontal;
transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
}
private void Jump(Vector2 direction)
{
if (direction==Vector2.up) {
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
}
private void CheckGround()
{
Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
}
private void CheckSwipe()
{
swipeDelta = Vector2.zero;
if (isSwiping)
{
if (Input.touchCount > 0)
swipeDelta = Input.GetTouch(0).position - tapPosition;
}
if (swipeDelta.magnitude > ddDelta)
{
if (SwipeEvent != null)
{
if (Mathf.Abs(swipeDelta.x) < Mathf.Abs(swipeDelta.y))
SwipeEvent(swipeDelta.y > 0 ? Vector2.up:Vector2.zero);
}
ResetSwipe();
}
}
private void ResetSwipe()
{
isSwiping = false;
tapPosition = Vector2.zero;
swipeDelta = Vector2.zero;
}
}