您的位置:首页 > 其它

Input.GetAxisRaw与Input.GetKeyDown (KeyCode.LeftArrow)

2017-11-30 11:09 363 查看
今天做俄罗斯方块时,控制左右键调用了不同的API

第一个API:Input.GetAxisRaw

作用是当按下左右键时候会返回-1,0,1  一定是这三个值其中的一个,但是当我们只按了一次方向加,会一直返回值,而不是只返回一次。因此做游戏的时候发现,会一次按键 方块移动多个格子。个人感觉这个方法在赛车类游戏或许会用的更多些。官方文档是这么样的:

Input.GetAxisRaw
public static float GetAxisRaw(string axisName);
Description

Returns the value of the virtual axis identified by axisName with no smoothing filtering applied.

The value will be in the range -1...1 for keyboard and joystick input. Since input is not smoothed, keyboard input will always be either -1, 0 or 1. This is useful if you want to do all smoothing of keyboard input processing yourself.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
void Update() {
float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
transform.Rotate(0, speed, 0);
}
}

第二个API:Input.GetKeyDown (KeyCode.LeftArrow)

作用是当按下左右键的第一次,返回true或者false ,而这个方法只会当按键触发的时刻有效,因此这个方法更适用于灵敏度需求不高的类型。Input.GetKeyDown
public static bool GetKeyDown(string name);
Description

Returns true during the frame the user starts pressing down the key identified by name.

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.

For the list of key identifiers see Input Manager. When dealing with input it is recommended to use Input.GetAxis and Input.GetButton instead since it allows end-users to configure the keys.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
void Update() {
if (Input.GetKeyDown("space"))
print("space key was pressed");

}
}
public static bool GetKeyDown(KeyCode key);
Description

Returns true during the frame the user starts pressing down the key identified by the key KeyCode enum parameter.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
void Update() {
if (Input.GetKeyDown(KeyCode.Space))
print("space key was pressed");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: