Input.GetButtonDown 获取按钮按下

static function GetButtonDown (buttonName : string) : bool

Description描述

Returns true during the frame the user pressed down the virtual button identified by buttonName.

在给定名称的虚拟按钮被按下的那一帧返回true。

You need to 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.

你需要在Update方法中调用这个方法,此后每一帧重置状态时,它将不会返回true除非用户释放此按键然后重新按下。

Use this only when implementing action like events IE: shooting a weapon. Use Input.GetAxis for any kind of movement behaviour.

只有当执行像武器射击这样的事件时才可用此方法,Input.GetAxis适用于各种运动行为。

using UnityEngine;
using System.Collections;

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

	}
}
// Instantiates a projectile whenever the user hits the Fire1 Button.
//实例化一个发射在每次用户点击Fire1按钮时

var projectile : GameObject;
	function Update () {
	if (Input.GetButtonDown ("Fire1")) {
		Instantiate (projectile, transform.position, transform.rotation);
	}
}
最后修改:2011年3月11日 Friday 21:20

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。