Home > database >  Touch controls in Unity3D need fixing
Touch controls in Unity3D need fixing

Time:04-25

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TouchPhaseDisplay : MonoBehaviour
{
[Header("GameObjects")]
public GameObject Pause_Menu;
public GameObject Gameplay_UI;

[Header("Touch Input")]
public Text directionTextComp;
private Touch theTouch;
private Vector2 touchStartPosition, touchEndPosition;
private string directionText;
private int tapCount = 0;
private float doubleTapTimer;

[Header("Movement")]
private Vector3 horitontalMovement;
private Vector3 verticalMovement;
public float speed;
private Vector3 direction;
public Rigidbody rb;

private void Start()
{
    rb = GetComponent<Rigidbody>();
}
    void LateUpdate()
{
    if (Input.touchCount > 0)
    {
        theTouch = Input.GetTouch(0);

        if (theTouch.phase == TouchPhase.Began)
        {
            touchStartPosition = theTouch.position;
        }
        else if (theTouch.phase == TouchPhase.Moved || theTouch.phase == TouchPhase.Ended)
        {

            touchEndPosition = theTouch.position;
            float x = touchEndPosition.x - touchStartPosition.x;
            float y = touchEndPosition.y - touchStartPosition.y;
            
            if (Mathf.Abs(x) == 0 && Mathf.Abs(y) == 0)
            {
                directionText = "Tapped";
            }
            else if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                directionText = x > 0 ? "Right" : "Left";
                horitontalMovement = x > 0 ? new Vector3(0, 0, -1) : new Vector3(0, 0, 1);
            }
            else
            {
                directionText = y > 0 ? "Up" : "Down";
                verticalMovement = y > 0 ? new Vector3(1, 0, 0) : new Vector3(-1, 0, 0);
            }

            direction = new Vector3(verticalMovement.x, 0, horitontalMovement.z);

            if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Ended)
            {
                directionText = "Touch Ended";
                direction = new Vector3(0, 0, 0);
            }
        }
    }
            rb.AddForce(direction * speed);
    directionTextComp.text = directionText;
    Debug.Log("tapCount");
    doubleTapDetector();
    
}

    private void doubleTapDetector()
{
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        tapCount  ;
    }
    if (tapCount > 0)
    {
        doubleTapTimer  = Time.deltaTime;
    }
    if (tapCount >= 2)
    {
        PauseMenu();
        doubleTapTimer = 0.0f;
        tapCount = 0;
    }
    if (doubleTapTimer > 0.2f)
    {
        doubleTapTimer = 0f;
        tapCount = 0;
        Debug.Log(doubleTapTimer);
    }
}
    private void PauseMenu()
{
    Time.timeScale = 0.00001f;
    Pause_Menu.SetActive(true);
    Gameplay_UI.SetActive(false);
}
}

This is the code I am using to move the player based on which direction the player swipes and it works but there is a big problem. If I swipe my finger left the player will start moving left which is fine except that if a swipe my finger upwards I'm still moving left constantly and the only way to stop moving left is to swipe right but now I'm constantly moving right. I think you see the problem and if there is a fix to this problem it would be greatly appreciated.

CodePudding user response:

You use separate vectors for verticalMovement and horitontalMovement, and never reset them. You probably want to store just a single direction vector.

  • Related