
GetAxis 関数
Tutorial
Beginner
+0XP
5 mins
(12)
Unity Technologies

Unity でゲームのために「軸」ベースの入力を取得する方法と、Input マネージャーで軸を変更する方法を学びます。
本チュートリアルは Beginner Scripting プロジェクトに含まれています。
前回のチュートリアルは:GetButton and GetKey
次のチュートリアルは:OnMouseDown
Languages available:
1. GetAxis 関数
AxisExample
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
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
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");
}
}