Version: 2022.2

Input.GetButtonDown

切换到手册
public static bool GetButtonDown (string buttonName);

描述

在用户按下由 buttonName 标识的虚拟按钮的帧期间返回 true。

Call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

仅在实现武器开火事件等操作时,才使用该函数。
对于任意类型的移动行为,请使用 Input.GetAxis

编辑、设置或删除按钮及其名称(例如“Fire1”): 1. Go to Edit > Project Settings > Input Manager to bring up the Input Manager. 2. 单击它旁边的箭头,展开 **Axis**。这将显示当前按钮的列表。您可以使用其中一个作为“buttonName”参数。 3. 展开列表中的某个项目后,可以访问和更改相关选项,例如按钮名称以及触发它的键、游戏杆或鼠标移动操作。 4. 有关按钮的更多信息,请参阅输入管理器页面。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject projectile; void Update() { if (Input.GetButtonDown("Fire1")) Instantiate(projectile, transform.position, transform.rotation); } }