Skip to main content

Class PlayerController

Manages player input and delegates actions to the associated controllable component.

[DisallowMultipleComponent]
public class PlayerController : MonoBehaviour

Inheritance​

Remarks​

This class is designed to work in conjunction with Unity's UnityEngine.InputSystem.PlayerInput component. It utilizes the Unity Input System's InputAction.CallbackContext to processes input actions such as movement, gear changes, camera toggling, and waypoint navigation, delegating them to the appropriate interfaces implemented by the controllable component. Upon enabling, it registers itself with the IPlayerSelectorService to allow centralized management.

Setup Instructions:

  • Attach a UnityEngine.InputSystem.PlayerInput component to the same GameObject.
  • Assign an Input Actions asset via the Inspector.
  • Set the Behavior to Invoke Unity Events in the PlayerInput component.
  • Expand the Events section in the Inspector and assign the corresponding public methods in this class to each input action.

Each input handling method in this class is designed to be assigned to a Unity Event corresponding to an input action. These methods accept an InputAction.CallbackContext parameter, which provides context about the input event.

For more information, refer to Unity's documentation on the PlayerInput component.

Methods​

GearInput(CallbackContext)​

Sends gear input to cycle or set the current gear of a controllable object (e.g., a vehicle).

public void GearInput(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

Called by the "Gear" input action. If the controllable implements IGearChangeable, the input value is used to shift gears (e.g., increment, decrement, or jump to a specific gear depending on input mapping).

MoveInput(CallbackContext)​

Sends movement input to the controllable component.

public void MoveInput(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

Called by the "Move" input action. Reads a Vector2 direction from the input and passes it to Vector2). Sends Vector2.zero on the canceled phase rather than relying on the input system to reset the value automatically. This is necessary because input device behaviour is inconsistent: some devices (e.g. gamepads) zero the axis on release while others (keyboard composites) may retain the last non-zero value until the next press, which would cause the actor to keep moving indefinitely.

Next_Waypoint(CallbackContext)​

Changes the selected outgoing link index one step forward on the current waypoint.

public void Next_Waypoint(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

In a multi-path waypoint network where each waypoint can lead to several others waypoints, this input increments the index of the active outgoing connection. It sends a WaypointIndexOffsetMessage with Offset = 1. The event aggregator is used rather than a direct reference to NavigationWaypointManager so that the same input works regardless of which pawn is active. Any scene object (vehicle or pedestrian) that subscribes to the message will respond, enabling centralised multi-actor route switching without tight coupling between the input layer and the navigation layer.

Prev_Waypoint(CallbackContext)​

Changes the selected outgoing link index one step backward on the current waypoint.

public void Prev_Waypoint(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

In a multi-path waypoint network where each waypoint can lead to several others waypoints, this input decrements the index of the active outgoing connection. It sends a WaypointIndexOffsetMessage with Offset = -1. The event aggregator is used rather than a direct reference to NavigationWaypointManager so that the same input works regardless of which pawn is active — see CallbackContext) for the full rationale.

RestartAction(CallbackContext)​

Triggers a level restart when the input is performed.

public void RestartAction(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

Called by the Unity Event linked to the "Restart" input action. Looks up GameMode via the UnityServiceLocator and calls RestartLevel().

WalkInput(CallbackContext)​

Toggles walking mode (slower movement) for controllables that support walking while the walk input is held.

public void WalkInput(InputAction.CallbackContext context)

Parameters​

NameTypeDescription
contextInputAction.CallbackContextThe input action context.

Remarks​

Bound to the "Walk" action. When the action is performed (key held), invokes Boolean) with true to walk instead of run; when canceled (key released), invokes it with false to resume normal movement speed.

  • Remarks
  • Methods
    • GearInput(CallbackContext)
    • MoveInput(CallbackContext)
    • Next_Waypoint(CallbackContext)
    • Prev_Waypoint(CallbackContext)
    • RestartAction(CallbackContext)
    • WalkInput(CallbackContext)