Input

The Ultimate Character Controller supports input via keyboard/mouse, controllers, and mobile input with virtual controls. The controller support uses Unity’s Input Manager which doesn’t have wide controller support – if your game is going to be making use of controllers we highly recommend an asset that is dedicated to controller support such as Rewired or InControl. The Ultimate Controller is integrated with both of these assets.

The Player Input component is base component that the Unity Input and integration components derive from. By using the Player Input component any new input can be added by just swapping out the component and the rest of the code does not need to change. If your character is an AI agent then no input component should be added – the AI implementation is responsible for interacting with the character.

Events

The “OnEnableGameplayInput” event can be sent if you’d like to disable (or enable) character and camera input. As an example the following will disable the input on the character specified by m_Character:

EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", false);

The Opsive.Shared.Events namespace must be included for the EventHandler class to be found.

using UnityEngine;
using Opsive.Shared.Events;

public class MyObject : MonoBehaviour
{
    [Tooltip("A reference to the Ultimate Character Controller character.")]
    [SerializeField] private GameObject m_Character;

    /// <summary>
    /// Disable the input.
    /// </summary>
    private void Start()
    {
        EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", false);
    }
}

API

The PlayerInput API follows the same standard as Unity’s input class with GetButton, GetButtonDown, GetButtonUp, and GetAxis methods. This component also allows for double press or long press detection with GetDoublePress and GetLongPress.

As an example the following code will check if the “Jump” button is down:

using UnityEngine;
using Opsive.Shared.Input;

public class MyObject : MonoBehaviour
{
    [Tooltip("A reference to the Ultimate Character Controller character.")]
    [SerializeField] private GameObject m_Character;

    private IPlayerInput m_PlayerInput;

    /// <summary>
    /// Initialize a reference to PlayerInput.
    /// </summary>
    private void Awake()
    {
        m_PlayerInput = m_Character.GetComponent<IPlayerInput>();
    }

    /// <summary>
    /// Check for the jump button press.
    /// </summary>
    private void Update()
    {
        if (m_PlayerInput.GetButtonDown("Jump")) {
            // Do Jump.
        }
    }
}

Inspected Fields

Horizontal Look Input Name

The name of the horizontal camera input mapping.

Vertical Look Input Name

The name of the vertical camera input mapping.

Look Vector Mode

Specifies how the look vector is assigned. If a mouse is being used then the look vector will almost always be based on the mouse movement. If a controller is used then the controller will supply the look input.

  • Smoothed: Apply a smoothing to the look vector. This smoothing interpolates the input over several frames to reduce jerky player input.
  • Unity Smoothed: Uses the input’s direct value.
  • Raw: Use the input’s direct (raw) value.
  • Manual: The look vector is assigned manually. This is useful for VR head movement.
Look Sensitivity

If using look smoothing, specifies how sensitive the mouse is. The higher the value to more sensitive.

Look Sensitivity Multiplier

If using look smoothing, specifies a multiplier to apply to the LookSensitivity value.

Smooth Look Steps

If using look smoothing, the amount of history to store of previous look values.

Smooth Look Weight

If using look smoothing, specifies how much weight each element should have on the total smoothed value (range 0-1). Reducing the weight to 0 is similar to setting the Look Vector Mode to Raw. A value of 1 will cause the result to be a simple average of all of the recently sampled smooth steps (and will feel very laggy).

Smooth Exponent

If using look smoothing, specifies an exponent to give a smoother feel with smaller inputs.

Look Acceleration Threshold

If using look smoothing, specifies a maximum acceleration value of the smoothed look value (0 to disable).

Controller Connected Check Rate

The rate (in seconds) the component checks to determine if a controller is connected. The only way to check if a controller is connected in Unity generates garbage so this value should be set to 0 if you do not plan on supporting controllers.

Connected Controller State

The state that should be activated when the controller is connected.

Force Input

Specifies if any input type should be forced. By default the controller will enable the virtual buttons for a mobile platform but this field allows you to force standalone input on a mobile platform. Virtual input can also be forced for a platform that normally does not use virtual controls.

Disable Cursor

Should the cursor be disabled?

Enable Cursor with Escape

Should the cursor be enabled when the escape key is pressed?

Prevent Look Vector Changes

If the cursor is enabled with escape should the look vector be prevented from updating?