【问题标题】:Holding space and jump保持空间和跳跃
【发布时间】:2021-07-02 22:43:29
【问题描述】:

我正在尝试制作我的第一款游戏,我想在每次跳跃之间创建一个计时器。例如,如果我按住空格键 0.5 秒,然后释放后我可以跳跃最小高度。如果我坚持3.5秒,那么我可以跳中等高度,依此类推。我不知道如何解决这个问题。总的来说,当玩家松开空格键时会跳跃而不是按下时。

我的代码如下所示:

IEnumerator Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && (holdTime >= 0f))
        {
            jumpForce = 2f;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && (holdTime >= 3f))
        {
            jumpForce = 5f;           
        }
        else if (Input.GetKeyDown(KeyCode.Space) && (holdTime == 5f))
        {
            jumpForce = 10f;
        }
        else
        {
            //force player to jump
            yield return new WaitForSeconds(0);
        }       
    }

【问题讨论】:

    标签: c# unity3d timer 2d


    【解决方案1】:

    尝试将其全部移至更新循环。

     float holdTimer = 0f;
    protected void Update()
    {
        //Check if you released first
        if (Input.GetKeyUp(KeyCode.Space))
        {
            //Jump based on the time held
            if (holdTimer >= 5f)
            {
                jumpForce = 10f;
            }
            else if (holdTimer >= 3f)
            {
                jumpForce = 5f;
            }
            else
            {
                jumpForce = 2f;
            }
            //reset the timer
            holdTimer = 0f;
        }
    
        if (Input.GetKey(KeyCode.Space))
        {
            //Increment the timer every frame.
            holdTimer += Time.deltaTime;
        }
    
    }
    

    【讨论】:

    • 谢谢!成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多