GetAxis

Evidence

Script #1

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”);
    }
}

Script #2

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”);
    }
}

Script #3

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”);
    }
}

Screencast

What I Learned

This tutorial taught me how to attach a script to an object that makes it move on an axis to the left or right and possibly up and down if you use the dual axis script. It works similarly to the GetButton script. One problem I had to solve was in the inspector view there is an option to change the range the object can move. At first I hadn’t changed the range from 0 so I was extremely confused as to why my object wouldn’t move. After figuring that out the rest was smooth sailing.

Leave a Reply

Your email address will not be published. Required fields are marked *