-
Notifications
You must be signed in to change notification settings - Fork 33
GetAxis
static float GetAxis(XboxAxis axis);
static float GetAxis(XboxAxis axis, XboxController controller);
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. -
XboxController controller
: 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. It is recommended to omit this parameter if you are making a single player game. Please refer to XboxController for all possible controllers.
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;
using XboxCtrlrInput;
public class PlayerExample : MonoBehavior
{
public XboxController playerNumber = XboxController.First;
public float walkingSpeed = 7.0f;
void Update()
{
// Move with the left stick
Vector3 newPosition = transform.position;
float axisX = XCI.GetAxis(XboxAxis.LeftStickX, playerNumber);
float axisY = XCI.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;
}
}