您的位置:首页 > 其它

Input.GetAxis("") Input.GetAxisRaw("")

2018-03-27 00:37 801 查看
Input.GetAxis 获取轴static function GetAxis (axisName : string) : floatDescription描述Returns the value of the virtual axis identified by axisName.根据坐标轴名称返回虚拟坐标系中的值。input.GetAxis 用法(GetAxis("Mouse X"),GetAxis("Mouse Y"),GetAxis("Mouse ScrollWheel"),GetAxis("Vertical "),GetAxis("Horizontal "),           GetAxis 是个方法,需要传参数,参数为string类型,参数如下:            一:触屏类                   1.Mouse X                       鼠标沿着屏幕X移动时触发                   2.Mouse Y                       鼠标沿着屏幕Y移动时触发                   3.Mouse ScrollWheel      当鼠标滚动轮滚动时触发            二:键盘操作类                   1.Vertical                         对应键盘上面的上下箭头,当按下上或下箭头时触发  ( w s )                   2.Horizontal                    对应键盘上面的左右箭头,当按下左或右箭头时触发 (a  d)      当在游戏运行的时候,按下你设置好的键盘,或拖拽鼠标就会返回 1和-1之间的值    如:  0 - 0.123 - 0.245 - 0.672 - 0.89 - 1.0
      值变化和快慢有关,
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}

Input.GetAxisRaw 获取原始轴

static function GetAxisRaw (axisName : string) : floatDescription描述Returns the value of the virtual axis identified by axisName with no smoothing filtering applied.通过坐标轴名称返回一个不使用平滑滤波器的虚拟坐标值。当在游戏运行的时候,按下你设置好的键盘就会返回 1和-1这两个值
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
void Update() {
float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
transform.Rotate(0, speed, 0);
}
}
原理:
返回输入设备在方法参数axisName所指定的轴上的位移量,该位移量由此次调用该方法时输入设备在轴上的位置与上次调用该方法时输入设备在轴上的位置相减得出。


检验代码

通过下面的代码对我的理解进行检验,在Unity中运行场景后,控制台始终输出
equal,初步证实我的猜想应该是正确的。而后我把Update方法改为FixedUpdate,再次运行,控制台仍然始终输出equal
再把FixedUpdate的频率从默认的每秒50次改成每秒5次,运行场景仍然输出equal。
private float lastFrameMousePositonY = 0;

void Update()
{
// 此帧中鼠标在Y方向上相对上一帧的偏移量
float offset = Input.mousePosition.y - lastFrameMousePositonY;

if(Input.GetAxis("Mouse Y") - offset < 0.0001)
{
print("equal");
}

lastFrameMousePositonY = Input.mousePosition.y;
}
输出结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐