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

今天做俄罗斯方块时,控制左右键调用了不同的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");
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值