简体   繁体   English

按住按钮时如何让我的播放器连续移动?

[英]How can i make my player continuously move when a button gets held down?

i want to make my player move to the left whenever a button gets held down, however when i tested my script in unity it only moves him one time.我想让我的播放器在按下按钮时向左移动,但是当我统一测试我的脚本时,它只会移动他一次。 I want to make him continuously move, does anyone know how to fix this?我想让他不断移动,有谁知道如何解决这个问题?

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

I would use a MonoBehavior for this: https://docs.unity3d.com/ScriptReference/Input.GetKey.html?_ga=2.41628450.1679355400.1598170423-1757949218.1598170423我会为此使用MonoBehaviorhttps : MonoBehavior = MonoBehavior

This is not an event you are looking for, so you need to check yourself every cycle if the key is pressed:这不是您要查找的事件,因此如果按键被按下,您需要在每个周期检查自己:

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

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

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