简体   繁体   English

统一向右和向左滑动错误

[英]unity swipe right and left bug

hello everyone and thanks for the help, I am creating a mobile game in unity that requires left and right swiping to move (subway surfers style) now the code is working just alright but when I start the game the player always shifts and starts on the very left lane and not stays in the middle as I want him to (after he shifts you can swipe just fine) any clues on how to fix it?大家好,感谢您的帮助,我正在统一创建一个手机游戏,需要左右滑动才能移动(地铁冲浪者风格)现在代码工作正常但是当我开始游戏时玩家总是移动并开始非常左边的车道,而不是像我希望他那样留在中间(在他换档后你可以滑动就好)关于如何解决它的任何线索?

the code:代码:

  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);

    }
}

You are not initializing pos to anything, so it will be set to its default value which is 0. Since 0 indicates the left lane, that is the one it will start on.您没有将pos初始化为任何东西,因此它将被设置为默认值 0。由于 0 表示左侧车道,因此它将从该车道开始。 Simply change int pos;只需更改int pos; to int pos = 1;int pos = 1; and it should start on the middle lane.它应该从中间车道开始。 Alternatively, you could make pos a public variable and set its value in the inspector, which makes it easier to tweak if you ever want to start in another lane.或者,您可以将pos设置为公共变量并在检查器中设置它的值,如果您想从另一条车道开始,这样可以更轻松地进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM