C# 无法使TouchPhase在Unity3D中工作
这是我的密码C# 无法使TouchPhase在Unity3D中工作,c#,android,ios,unity3d,C#,Android,Ios,Unity3d,这是我的密码 using UnityEngine; using System.Collections; public class cube : MonoBehaviour { bool start; // Use this for initialization void Start () { start = false; } // Update is called once per frame void Update () {
using UnityEngine;
using System.Collections;
public class cube : MonoBehaviour {
bool start;
// Use this for initialization
void Start () {
start = false;
}
// Update is called once per frame
void Update () {
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended) {
start = false;
}
if (start) {
transform.RotateAround (Vector3.zero, Vector3.left, 1);
}
else
transform.RotateAround (Vector3.zero, Vector3.right, 1);
}
}
触摸屏幕时,“开始”变量应设置为true,立方体应开始顺时针旋转,当您放开屏幕时,立方体应开始逆时针旋转,但仅在触摸屏幕时顺时针旋转,当我放开屏幕时立方体停止旋转
还有一件事,当我通过触摸屏幕将变量设置为true时,立方体应该继续旋转,因为start变量现在为true,但一旦我停止触摸屏幕,立方体就会停止旋转。它适用于键盘按钮,但不适用于触摸屏
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
if (start) {
transform.RotateAround (Vector3.zero, Vector3.left, 1);
}
我已经花了几个小时在这上面,但仍然无法了解问题所在。您的代码在这里:
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended) {
start = false;
}
如果屏幕上没有触摸,会发生什么?是的,你会得到一个错误,因为游戏试图访问一些不存在的东西。如果您在Unity上模拟它,它将打印在控制台上,但当您使用自己的设备时,不会收到发生错误的通知。将
touchCount
添加到代码中:
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch (0).phase == TouchPhase.Ended) {
start = false;
}
}