返回由 axisName
标识的虚拟轴的值。
对于键盘和游戏杆输入设备,该值将处于 -1...1 的范围内。
该值的含义取决于输入控制的类型,例如,对于游戏杆的水平轴,值为 1 表示游戏杆向右推到底,值为 -1 表示游戏杆向左推到底;值为 0 表示游戏杆处于中性位置。
如果将轴映射到鼠标,该值会有所不同,并且不会在 -1...1 的范围内。此时,该值为当前鼠标增量乘以轴灵敏度。通常,正值表示鼠标向右/向下移动,负值表示鼠标向左/向上移动。
该值与帧率无关;使用该值时,您无需担心帧率变化问题。
To set up your input or view the options for axisName
, go to Edit > Project Settings > Input Manager. This brings up the Input Manager. Expand Axis to see the list of your current inputs. You can use one of these as the axisName
. To rename the input or change the positive button etc., expand one of the options, and change the name in the Name field or Positive Button field. Also, change the Type to Joystick Axis. To add a new input, add 1 to the number in the Size field.
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,因此没有步幅。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.