-
Notifications
You must be signed in to change notification settings - Fork 30
GetAxis
static float GetAxis(XboxAxis axis);
static float GetAxis(XboxAxis axis, int controllerNumber);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.
-
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.
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;
}
}