Unity 2D - 点击图像加速汽车

Unity 2D - Tap Image to accelerate car

我正在制作一个 Hill Climb Racing 克隆作为学校项目。目前我的车在我的编辑器中使用 A 和 D 键加速 (movement = Input.GetAxis("Horizontal");),但是游戏必须针对 Android,因此就像在 OG 游戏中一样,我为刹车和踏板添加了 2 个精灵并向它们添加了事件系统,例如:

public void OnPointerDown(PointerEventData eventData)
{
    gas.sprite = OnSprite_gas;
    is_clicking = true;

}

而且我不知道如何将加速度更改为单击并按住气体图像时以及如何在按住制动器时制动(但不向后行驶)。

看来你的方向是正确的。

在汽车的Update()方法中,您将要检查是否设置了刹车或油门按钮is_clicking 属性,并处理运动力。 这可能看起来像:

void Update()
{
    if (accelerator.is_clicking)
    {
        movement = new Vector3(1f, 0f, 0f) * speed;
    }
    else if (brake.is_clicking)
    {
        movement = new Vector3(-1f, 0f, 0f) * speed;
    }
    else
    {
        movement = new Vector3(0f, 0f, 0f);
    }
}

void FixedUpdate()
{
    rb.AddForce(movement * Time.fixedDeltaTime);
}

然后您可以检查速度是否接近于 0 以停止施加制动力。