触摸行为与按A和D相同

问题描述 投票:0回答:1

所以我有这个游戏,您用“ A”和“ D”左右移动汽车。我使用以下代码执行此操作:在“ A”上按:

characterBody.AddForce(-moveSpeed, 0, 0, ForceMode.Impulse);

在“ D”上按:

characterBody.AddForce(moveSpeed, 0, 0, ForceMode.Impulse);

我现在正在尝试使用屏幕两侧的触摸将该游戏转移到手机上。这是我的代码,使用相同的概念:

void Update()
{
    int i = 0;
    //loop over every touch found
    while (i*2 < Input.touchCount)
    {
        if (Input.GetTouch(i).position.x > ScreenWidth / 2)
        {
            //move right
            //RunCharacter(1.0f);
            characterBody.velocity = Vector3.zero;

            characterBody.AddForce(moveSpeed, 0, 0, ForceMode.Impulse);
        }
        if (Input.GetTouch(i).position.x < ScreenWidth / 2)
        {
            //move left
            //RunCharacter(-1.0f);
            characterBody.velocity = Vector3.zero;
            characterBody.AddForce(-moveSpeed, 0, 0, ForceMode.Impulse);
        }
        ++i;
    }
}

当我执行此代码时,无论我将moveSpeed变量设置为多高或低,它都不会产生相同的效果。箭头键/“ A”和“ D”的含义如下:https://infinitecarspeeder.netlify.com。我希望它具有那种效果,但是它没有ForceMode.Impulse效果,并且切换方向非常慢。非常感谢!

编辑:

这是我用于键盘移动的代码(工作正常):

void FixedUpdate(){
    transform.Translate (Vector3.forward * Time.deltaTime * forwardSpeed);
    if(Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.LeftArrow)){

        MoveLeft();
    }
    if(Input.GetKey(KeyCode.D)||Input.GetKey(KeyCode.RightArrow)){

        MoveRight();
    }
}

public void MoveLeft()
{

    rb.AddForce(-10.75f, 0, 0, ForceMode.Impulse);
}
public void MoveRight()
{

    rb.AddForce(10.75f, 0, 0, ForceMode.Impulse);
}
unity3d mobile touch
1个回答
1
投票

使用键盘时,是否将字符速度重置为0?如果不这样做,那可能会在感觉上有所不同。如果在每次更新时将速度重置为零,则汽车无法移动的最大速度为1.0f。如果您不这样做,那么汽车的行驶速度将超过1.0f,这可以解释速度的差异。

© www.soinside.com 2019 - 2024. All rights reserved.