【问题标题】:unity swipe right and left bugunity左右滑动bug
【发布时间】:2020-07-28 18:32:34
【问题描述】:

大家好,谢谢大家的帮助,我正在统一制作一个手机游戏,需要左右滑动才能移动(地铁冲浪风格) 现在代码工作正常,但是当我开始游戏时,玩家总是在最左边的车道上移动,而不是像我想要的那样停留在中间(在他移动后你可以滑动就好) 关于如何修复它的任何线索?

代码:

  private float startpos;
    private int pos;
    public float[] positionsset;
    public GameObject player;
    private Vector3 velocityf = Vector3.zero;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        
        if (Input.GetMouseButtonDown(0))
        {
            startpos = Input.mousePosition.x;
        }
        /////////////////////////////////////////////////////
        else if (Input.GetMouseButtonUp(0))
        {
            if (Input.mousePosition.x - startpos > 0 && pos < 2)
            {
                pos++;
            }
            else if (Input.mousePosition.x - startpos < 0 && pos > 0)
            {
                pos--;
            }
        }

        player.transform.position = Vector3.SmoothDamp(player.transform.position, new Vector3(positionsset[pos], player.transform.position.y, player.transform.position.z), ref velocityf, 0.1f);

    }
}

【问题讨论】:

    标签: c# unity3d mobile game-engine


    【解决方案1】:

    您没有将pos 初始化为任何内容,因此它将被设置为其默认值0。由于0 表示左侧车道,因此它将从左侧车道开始。只需将int pos; 更改为int pos = 1;,它应该从中间车道开始。或者,您可以将 pos 设为公共变量并在检查器中设置其值,这样如果您想从另一个通道开始,调整起来会更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 2010-10-10
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多