【问题标题】:How can i make my player continuously move when a button gets held down?当一个按钮被按住时,我怎样才能让我的播放器连续移动?
【发布时间】:2020-08-23 06:12:49
【问题描述】:

我想让我的玩家在按下按钮时向左移动,但是当我统一测试我的脚本时,它只会让他移动一次。我想让他不断移动,有谁知道如何解决这个问题?

 public void LeftMove()//Gets called everytime my button is held down
{
    rb.AddForce(120 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}

【问题讨论】:

  • 能否分享您的完整代码?谢谢!

标签: c# unity3d


【解决方案1】:

我会为此使用MonoBehaviorhttps://docs.unity3d.com/ScriptReference/Input.GetKey.html?_ga=2.41628450.1679355400.1598170423-1757949218.1598170423

这不是您要查找的事件,因此您需要在每个循环中检查自己是否按下了键:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.DownArrow))
        {
            rb.AddForce(120 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

【讨论】:

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