public static float GetAxis (string axisName);

描述

返回由 axisName 标识的虚拟轴的值。

对于键盘和游戏杆输入设备,该值将处于 -1...1 的范围内。

该值的含义取决于输入控制的类型,例如,对于游戏杆的水平轴,值为 1 表示游戏杆向右推到底,值为 -1 表示游戏杆向左推到底;值为 0 表示游戏杆处于中性位置。

如果将轴映射到鼠标,该值会有所不同,并且不会在 -1...1 的范围内。此时,该值为当前鼠标增量乘以轴灵敏度。通常,正值表示鼠标向右/向下移动,负值表示鼠标向左/向上移动。

该值与帧率无关;使用该值时,您无需担心帧率变化问题。

要设置输入或查看 axisName 的选项,请转到 Edit > Project Settings > Input。这将调出 Input Manager。展开 Axis 可查看当前输入的列表。您可以使用其中一个作为 /axisName/。要重命名输入或更改 Positive Button 等,请展开其中一个选项,然后在 Name 字段或 Positive Button 字段中更改名称。此外,请将 Type 更改为 Joystick Axis。要添加新的输入,请将 Size 字段中的数字加 1。

using UnityEngine;
using System.Collections;

// A very simplistic car driving on the x-z plane.

public class ExampleClass : MonoBehaviour { public float speed = 10.0f; public float rotationSpeed = 100.0f;

void Update() { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 float translation = Input.GetAxis("Vertical") * speed; float rotation = Input.GetAxis("Horizontal") * rotationSpeed;

// Make it move 10 meters per second instead of 10 meters per frame... translation *= Time.deltaTime; rotation *= Time.deltaTime;

// Move translation along the object's z-axis transform.Translate(0, 0, translation);

// Rotate around our y-axis transform.Rotate(0, rotation, 0); } }
using UnityEngine;
using System.Collections;

// Performs a mouse look.

public class ExampleClass : MonoBehaviour { float horizontalSpeed = 2.0f; float verticalSpeed = 2.0f;

void Update() { // Get the mouse delta. This is not in the range -1...1 float h = horizontalSpeed * Input.GetAxis("Mouse X"); float v = verticalSpeed * Input.GetAxis("Mouse Y");

transform.Rotate(v, h, 0); } }

注意:水平范围和垂直范围从 0 变为 +1 或 -1,以 0.05f 的步幅增加/减少。GetAxisRaw 立即从 0 变为 1 或 -1,因此没有步幅。

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961