【问题标题】:Why is my Unity 2D character not flipping?为什么我的 Unity 2D 角色不翻转?
【发布时间】:2021-08-09 04:57:32
【问题描述】:

我的角色移动得很好。但它没有翻转。我的脚本有什么问题吗? 这是我第一次开发用于练习的演示游戏,如果有人帮助我,我会很高兴。

public class movement2 : MonoBehaviour
{
    public float speed = 100;

    void Start()
    {
    }

    void Update()
    {
        var inputX = Input.GetAxis("Horizontal");
        Vector3 movement = new Vector3(inputX, 0, 0)*speed;
        movement *= Time.deltaTime;
        transform.Translate(movement);

        Vector2 CharacterScale = transform.localScale;

        if (inputX < 0) 
        { 
            CharacterScale.x = -1;
        }

        if (inputX > 0)
        {
            CharacterScale.x = 1;
        }
    }
}

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    因为您没有分配 transform.localscale 的值,所以您只需更改局部变量。

        if (inputX < 0) 
        { 
            transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
        }
    
        if (inputX > 0)
        {
            transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多