This guide walks through creating a playable player in a scene and wiring basic UI hooks.
Prerequisites
- Unity 2019.4+ (2020.3 recommended).
- (Optional) Unity Input System package if you want the default key bindings.
Create the player
- Create an empty GameObject named
Playerat the scene origin. - Add a built-in
CharacterControllercomponent to thePlayerGameObject. - Add the following scripts to the same GameObject (component order is not strict, but dependencies assume
PlayerInputandPlayerMovementare present):PlayerInputPlayerMovementPlayerStamina(optional; adds sprinting/stamina behavior)
- Create a child GameObject called
CameraHolderand attach yourCameraas its child. AttachMouseLooktoCameraHolderor theCameraandHeadBobto theCameraobject.
Recommended inspector settings (starting points)
PlayerMovement:- WalkSpeed: 2.0
- RunSpeed: 4.0
- JumpHeight: 1.2
MouseLook:- MouseSensitivity: 0.2
- LookSmoothTime: 0.06
HeadBob:- UseHeadBob: true
- BobFrequency: 12
Input system notes
PlayerInputconfigures the Input System actions at runtime with keyboard and mouse bindings. If you don't use the Unity Input System package you can replacePlayerInputinternals while keeping the public properties (Move,Look,RunHeld,ConsumeJump()).
UI integration examples
- Bind stamina UI via delegate:
var stamina = player.GetComponent<PlayerStamina>();
stamina.OnStaminaStateChanged += (percent, sprinting) => staminaBar.fillAmount = percent;- Or subscribe to published event (when
GameEventBuspresent):
// your event system should receive StaminaChangedEventPausing movement for UI
- Call
PlayerMovement.PauseMovement()to temporarily freeze horizontal movement while allowing gravity to continue (useful for dialog or menus). CallResumeMovement()to restore control.
Troubleshooting
- Camera snaps on enable: ensure
MouseLookhas itsplayerBodyor camera reference set and smoothing values are reset when enabling/disabling. - No input: install Unity Input System package or provide a compatible
PlayerInputimplementation.
Advanced
- Replace
Compat/UpdateManagerandGameEventBuswith your game's existing update/event systems for tighter integration.