在每次触摸之间更改X的值



我正在学习C#,帮帮我,我正在尝试传递我用来发出键盘声音的代码,但当我使用触摸时,有时它会停止检测或增加值,我已经在一些设备上尝试过了,同样的事情也发生了,我希望有人告诉我如何让它在没有故障的情况下正常工作。

这是我在电脑上使用的代码

private void FixedUpdate()
{
if (Input.GetKey(KeyCode.D))
{
direccion = 5;
}
else if (Input.GetKey(KeyCode.A))
{

direccion = -5;
}
if (run2 == true)
{
gameObject.transform.Translate(direccion * Time.deltaTime, velocidad * Time.deltaTime, 0);
}

这是我试图用于手机的代码。

private void FixedUpdate()
{
foreach (Touch touch in Input.touches)
if (touch.phase == TouchPhase.Began)
{
direct = true;
if (direct == true)
{
getTouch++;
direct = false; 
}
if (getTouch ==1)
{
direccion = 5;
}
else if (getTouch >= 2)
{
direccion = -5;
getTouch = 0;
direct = true;
}
}

if (run2 == true)
{
gameObject.transform.Translate(direccion * Time.deltaTime, velocidad * Time.deltaTime, 0);
}

设置之后

direct = true;

下一个

if(direct == true)

永远都是这样。

一般来说,不使用计数器等,只需执行例如

private int direction = 5;

然后以后只交替符号进行

if (touch.phase == TouchPhase.Began)
{
direccion *= -1;
}

一般情况下:

您的第一个代码是可以的,因为它使用连续输入GetKey,这在每帧中都是真的。

但是无论何时使用单个事件输入,如GetKeyDown,或者在您的情况下使用仅在一个帧内为trueTouchPhase.Began您都应该在Update中获得输入!

由于CCD_ 6可能不是每帧都被调用,所以您可能只是在CCD_;你错过了这个触摸输入。

所以宁愿使用

private int direction = 5;
private void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
direccion *= -1;
}
}
}
private void FixedUpdate ()
{
if (run2)
{
transform.Translate(direccion * Time.deltaTime, velocidad * Time.deltaTime, 0);
}
}

最新更新