Input.GetAxis(““) Input.GetAxisRaw(““)

Input.GetAxis 获取轴

static function GetAxis (axisName : string) : float

Description描述

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) : float

Description描述

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。

 
  1. private float lastFrameMousePositonY = 0;

  2.  
  3. void Update()

  4. {

  5. // 此帧中鼠标在Y方向上相对上一帧的偏移量

  6. float offset = Input.mousePosition.y - lastFrameMousePositonY;

  7.  
  8. if(Input.GetAxis("Mouse Y") - offset < 0.0001)

  9. {

  10. print("equal");

  11. }

  12.  
  13. lastFrameMousePositonY = Input.mousePosition.y;

  14. }

输出结果:

输出结果截图

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值