---
title: "GetAxis"
content_type: tutorial
url: "https://learn.unity.com/tutorial/getaxis-o-1?language=zh-cn"
language: zh-cn
difficulty: beginner
duration_minutes: 5
unity_version:
  - "4.x"
supported_unity_versions:
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/getaxis-o-1.md?language=zh-cn&version=4.x"
available_languages:
  - name: "en"
    url: "https://learn.unity.com/tutorial/getaxis-o.md?language=en"
  - name: "es"
    url: "https://learn.unity.com/tutorial/getaxis-e.md?language=es"
  - name: "ja"
    url: "https://learn.unity.com/tutorial/getaxis-guan-shu.md?language=ja"
  - name: "ko"
    url: "https://learn.unity.com/tutorial/getaxis-d.md?language=ko"
  - name: "zh-cn"
    url: "https://learn.unity.com/tutorial/getaxis-o-1.md?language=zh-cn"
last_updated: "2026-08-04T14:23:50.000Z"
---

# GetAxis

学习使用 GetAxis 获取基于轴的输入，并在 Input Manager 中配置这些轴的参数。

如何在 Unity 中为游戏获取基于轴的输入，以及如何通过 Input Manager 修改这些轴

本教程包含在“[初级编程](https://learn.unity.com/project/chu-ji-bian-cheng?language=zh)”项目中。

上一教程：[GetButton 和 GetKey](https://learn.unity.com/tutorial/getbutton-he-getkey?language=zh&projectId=5e3afd88edbc2a2998252c77)

下一教程：[OnMouseDown](https://learn.unity.com/tutorial/onmousedown-1?language=zh&projectId=5e3afd88edbc2a2998252c77)

## 1. GetAxis

[Video](https://connect-mediagw.unity.com/h1/20200206/videos/917401c4-eb37-411a-8da7-fa73637aaf69_17_C__GetAxis_in_Unity!___Beginner_Scripting_Tutorial.mp4) ([Transcript (zh_cn)](https://connect-mediagw.unity.com/h1/20200206/7d28ec1b-bf75-40f8-ba63-b68c55309f2f_17_C__GetAxis_in_Unity____Beginner_Scripting_Tutorial___Chinese.vtt))

## **AxisExample**

```csharp
using UnityEngine;
using System.Collections;

public class AxisExample : MonoBehaviour
{
    public float range;
    public GUIText textOutput;
    
    
    void Update () 
    {
        float h = Input.GetAxis("Horizontal");
        float xPos = h * range;
        
        transform.position = new Vector3(xPos, 2f, 0);
        textOutput.text = "Value Returned: "+h.ToString("F2");  
    }
}
```

## **AxisRawExample**

```csharp
using UnityEngine;
using System.Collections;

public class AxisRawExample : MonoBehaviour
{
    public float range;
    public GUIText textOutput;
    
    
    void Update () 
    {
        float h = Input.GetAxisRaw("Horizontal");
        float xPos = h * range;
        
        transform.position = new Vector3(xPos, 2f, 0);
        textOutput.text = "Value Returned: "+h.ToString("F2");  
    }
}
```

## **DualAxisExample**

```csharp
using UnityEngine;
using System.Collections;

public class DualAxisExample : MonoBehaviour 
{
    public float range;
    public GUIText textOutput;
    
    
    void Update () 
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float xPos = h * range;
        float yPos = v * range;
        
        transform.position = new Vector3(xPos, yPos, 0);
        textOutput.text = "Horizontal Value Returned: "+h.ToString("F2")+"\nVertical Value Returned: "+v.ToString("F2");    
    }
}
```
