Unity3D 中的触控需要修复
Touch controls in Unity3D need fixing
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);
}
}
这是我用来根据玩家滑动的方向移动玩家的代码,它可以工作,但有一个大问题。如果我向左滑动手指,播放器将开始向左移动,这很好,除非向上滑动我的手指,我仍然不断向左移动,停止向左移动的唯一方法是向右滑动,但现在我一直在向右移动.我想您已经看到了问题,如果可以解决此问题,我们将不胜感激。
您为 verticalMovement
和 horitontalMovement
使用了不同的向量,并且从不重置它们。您可能只想存储一个方向向量。
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);
}
}
这是我用来根据玩家滑动的方向移动玩家的代码,它可以工作,但有一个大问题。如果我向左滑动手指,播放器将开始向左移动,这很好,除非向上滑动我的手指,我仍然不断向左移动,停止向左移动的唯一方法是向右滑动,但现在我一直在向右移动.我想您已经看到了问题,如果可以解决此问题,我们将不胜感激。
您为 verticalMovement
和 horitontalMovement
使用了不同的向量,并且从不重置它们。您可能只想存储一个方向向量。