Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

GetAxis

Jibran Syed edited this page Sep 5, 2013 · 9 revisions

<- Go Back

Signature

static float GetAxis(XboxAxis axis);
static float GetAxis(XboxAxis axis, int controllerNumber);

Description

GetAxis() returns a float value of the specified axis. If the specified axis is a trigger, it will return a number between 0.0f and 1.0f. For all other axis, it will return a number between -1.0f and 1.0f. If GetAxis() returns 0.0f, the specified axis is probably in its resting position (this varies depending how the Dead value was defined). GetAxis() can be used to move a character in 360 direction, for example.

Parameters

  • XboxAxis axis : An identifier for the specified Xbox axis you want to test. Please refer to XboxAxis for all the available axis. Usually one controller stick is composed of two axis each.

  • int controllerNumber : An identifier for the specific controller on which to test the axis. If this parameter isn't provided, all connected controllers will be tested for the specified axis.

Example

The example demonstrates the use of GetAxis() if you wanted a particular player to move in 360 along the X/Z plane with the Xbox's left stick. Two axis are required for this.

using UnityEngine;

public class PlayerExample : MonoBehavior
{
    public int playerNumber = 1;
    public float walkingSpeed = 7.0f;    

    void Update()
    {
        // Move with the left stick
        Vector3 newPosition = transform.position;
        
        float axisX = XboxCtrlrInput.GetAxis(XboxAxis.LeftStickX, playerNumber);
        float axisY = XboxCtrlrInput.GetAxis(XboxAxis.LeftStickY, playerNumber);
        
        float newPosX = newPosition.x + (axisX * walkingSpeed * Time.deltaTime);
        float newPosZ = newPosition.z + (axisY * walkingSpeed * Time.deltaTime);

        newPosition = new Vector3(newPosX, transform.position.y, newPosZ);
        transform.position = newPosition;
    }
}

<- Go Back

Clone this wiki locally