首页 > 解决方案 > 玩家不再向左移动

问题描述

想要翻转火点,现在玩家不再向左移动。

void Update()
{

    float h = Input.GetAxis("Horizontal");

    transform.Translate(Vector3.right * h * movSpeed * Time.deltaTime);

    if (h > 0 && !facingRight)
    {
        Flip();
    }
    else if (h < 0 && facingRight)
    {
        Flip();
    }

}

private void Flip()
{
    facingRight = !facingRight;
    transform.Rotate(0f, 180f, 0f);
}

标签: c#unity3d

解决方案


Transform.Translate如果不使用第二个参数,则在相对于对象的坐标空间中移动。因为你在翻转它,所以你总是朝着同一个方向移动。链接的文档指出:

IfrelativeTo被忽略或设置为Space.Self相对于变换的局部轴应用移动。

您想Space.World用作第二个参数(默认为Space.Self):

transform.Translate(Vector3.right * h * movSpeed * Time.deltaTime, Space.World);

推荐阅读