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

GetAxisRaw

Jibran Syed edited this page Jan 30, 2016 · 10 revisions

<- Back to Coding References

Signature

static float GetAxisRaw(XboxAxis axis);
static float GetAxisRaw(XboxAxis axis, XboxController controller);

Description

GetAxisRaw() is the same as GetAxis() except it doesn't apply Unity's axis smoothing filter. It 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 GetAxisRaw() returns 0.0f, the specified axis is probably in its resting position (this varies depending how the Dead value was defined). GetAxisRaw() can be used to move a character in 360° direction, for example. If desired, you can provide your own smoothing filter.

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.

  • 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.

Example

The example demonstrates the use of GetAxisRaw() 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. This example is the same as the GetAxis() example except you provide your own smoothing algorithm.

using UnityEngine;
using XboxCtrlrInput;

public class PlayerExample : MonoBehavior
{
    public XboxController playerNumber = XboxController.First;
    public float walkingSpeed = 7.0f;
    
    // For use in your own axis smoothing algorithm
    private float prevAxisX = 0.0f;
    private float prevAxisY - 0.0f;

    private float axisX = 0.0f;
    private float axisY = 0.0f;

    void Update()
    {
        // Move with the left stick
        Vector3 newPosition = transform.position;
        
        prevAxisX = axisX;
        prevAxisY = axisY;
        
        // Get the axis
        axisX = XCI.GetAxis(XboxAxis.LeftStickX, playerNumber);
        axisY = XCI.GetAxis(XboxAxis.LeftStickY, playerNumber);
        
        // Smooth the axis
        axisX = AxisSmoothing(axisX, prevAxisX);
        axisY = AxisSmoothing(axisY, prevAxisY);
        
        // Apply new position
        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;
    }

    // For applying your own smoothing filter.
    private float AxisSmoothing(float currAxis, float prevAxis)
    {
        // Perform your smoothing filter voodoo here.
        float result = voodoo(currAxis, prevAxis);
        return result;
    }
}

Axis Map

AxisMap

<- Back to Coding References

Clone this wiki locally