Skip to main content

PlayerController

An abstract class in Root.Township, the global namespace.

The body a player controls: head, hands, camera, health and locomotion, as a networked prefab. Exists on both sides for every player; PlayerController.Current is the local one and Player.PlayerController links each Player to its body. Subclasses include PlayerCharacter (the normal VR body) and the fly cam.

Constructors​

MemberWhat it is
protected PlayerController()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
protected static Logger loggerNLog logger shared by player controller classes.
protected IPostProcessController postProcessControllerThe injected post process controller (colour saturation, resets).
18 private and internal fields
MemberWhat it is
private float[] androidCullDistancesPer-layer cull distances for Android. Not used in this class.
private float androidFarClipingPlaneCamera far clip distance for Android (800 by default). Not used in this class.
private ClearGBuffer clearGBufferClient: G-buffer clear helper made for the local player's camera when it links.
private static PlayerController currentBacking field for Current, the local player's controller.
private HealthObject healthTargetBacking field for Health.
private Controller leftControllerBacking field for LeftController.
private MonoBehaviour[] localPlayerBehavioursChild behaviours implementing IInitializeAsLocalPlayer, initialised when this becomes the local player's controller.
private LocomotionController locomotionControllerBacking field for LocomotionController.
private PlayerMessageDisplay messageDisplayBacking field for MessageDisplay.
private Camera playerCameraBacking field for Camera.
private VariableIntensityPlayerLight playerLightBacking field for PlayerLight.
private PlayerControllerSettings playerSettingsBacking field for Settings.
private MonoBehaviour[] remotePlayerBehavioursChild behaviours implementing IInitializeAsRemotePlayer, initialised when this controller belongs to someone else (and on the server).
private Controller rightControllerBacking field for RightController.
private PlayerScreenFader screenFaderBacking field for ScreenFader.
private NetworkEntityHandler setEntityOwnerHandlerEntity handler field. Not used in this class.
private MethodSync syncPlayerMessage sync for SyncPlayer, sent to each player that connects to the entity.
private Target targetBacking field for Target.

Properties​

MemberWhat it is
virtual Vector3 BodyCenter { get; }World position of the body target from the BodyController.
virtual BodyController BodyController { get; }The body controller. Null in the base class; the character subclasses override it.
Camera Camera { get; }The player's camera. Exists on the server too but is only rendered for the local player.
static PlayerController Current { get; private set; }The local player's controller, or null. Changing it raises CurrentPlayerChanged and locks rendering while null.
Vector3 FeetOffset { get; }Head offset from the root with the height removed, in world space.
abstract Transform Head { get; }The head transform. Each subclass supplies it.
abstract Transform HeadFollow { get; }Transform that follows the head, supplied by the subclass.
virtual float HeadHeight { get; }Head height. One in the base class.
Vector3 HeadOffset { get; }The head's local position turned into a world space vector from the root.
HealthObject Health { get; }The HealthObject for this body.
CharacterInfo Info { get; private set; }CharacterInfo for this body, created in Awake.
virtual float InputScale { get; }Scale applied to input. One in the base class.
bool IsAlive { get; set; }False while dead. Set by the dead state; health ignores damage when false.
bool IsDowned { get; set; }True while the player is in the downed state.
bool IsIgnoringDamage { get; set; }When true, damage (not healing) to this player is ignored by PlayerHealth.
virtual bool IsLocalPlayer { get; private set; }True for the body this machine controls. Set when it becomes Current.
virtual bool IsShowingName { get; }Whether other players see a name tag. True unless overridden (the fly cam uses it for invisibility).
bool IsSpirit { get; set; }True while the player is in spirit form after death.
Controller LeftController { get; }The left hand Controller.
LocomotionController LocomotionController { get; }The LocomotionController that moves this body.
PlayerMessageDisplay MessageDisplay { get; }The PlayerMessageDisplay that shows text messages in front of the player.
Player NetworkPlayer { get; private set; }The Player this body belongs to. Null until linked.
NetworkPrefab NetworkPrefab { get; }The body's NetworkPrefab.
virtual Vector3 PlayerFeetPosition { get; }World position under the head at the root's height.
VariableIntensityPlayerLight PlayerLight { get; }The light the player carries around in dark places.
virtual Vector3 PlayerSavePosition { get; }Position written to the player's save. The feet position unless overridden.
Vector3 RealFeetOffset { get; }RealFeetPosition relative to the root.
virtual Vector3 RealFeetPosition { get; }Actual feet position. Same as PlayerFeetPosition unless a subclass overrides it.
Controller RightController { get; }The right hand Controller.
virtual float Scale { get; }The body's uniform scale from its local scale.
PlayerScreenFader ScreenFader { get; }The screen fader, used to hold a black screen while chunks load.
PlayerControllerSettings Settings { get; }The PlayerControllerSettings asset.
Target Target { get; }The AI Target component creatures use to find this player.
protected virtual SaveMode SaveMode { get; }Save mode given to the prefab in Awake. Ignore in the base class, so the body is not saved into chunks.
1 private and internal property
MemberWhat it is
private int IBuildRendererBakeable.SortingOrder { get; }Build bake order for this controller, always 0.

Events​

MemberWhat it is
event CombatEventHandler CausedCombatRaised when this player hurts something, with the target, squared distance and damage.
static event Action<PlayerController> CurrentPlayerChangedStatic event raised whenever Current changes.
event Action hasBeenMovedRaised by MarkMovedForViewComfort.
event Action LinkedToLocalPlayerRaised when the body is linked to its player (local or remote, despite the name).
static event Action<PlayerController> LinkedToRemotePlayerStatic event raised with the controller each time any body is linked to a player.
static event PlayerControllerHandler LocalPlayerSetStatic event raised when a body linked as the local player is set up.
event PlayerControllerHandler OnAboutToLeaveRaised by OnPrePlayerLeaving before the player leaves the server.

Methods​

MemberWhat it is
virtual void Bake(bool isSaving)Build step: clears the camera background colour.
void ForceTeleportTo(Vector3 target)Teleports the body to a position, using the effect controller's teleport if there is one, else a locomotion reposition.
override void Initialize()Registers the sync that tells clients which Player this body belongs to.
void InitializePlayerBehaviours()Runs IInitializeAsLocalPlayer or IInitializeAsRemotePlayer on every child component that has it, depending on whether this is the local player.
void InjectPostProcess(IPostProcessController postProcessController)Dependency injection hook that hands in the post process controller.
static bool IsSavingInteractor(Interactor interactor)True if the hand is holding something whose base save mode is SaveMode.Base, so it is saved with the player.
void LinkToPlayer(Player networkPlayer, PlayerInput left, PlayerInput right)Ties this body to a Player, sets up both hands and player behaviours, and makes it Current if local. Server: grants spawn impact invincibility.
virtual void LoadFrom(PlayerSave save)Hook to load from a PlayerSave. Empty in the base class.
void MarkMovedForViewComfort()Raises hasBeenMoved, used by the vision comfort effect after forced movement.
void OnCausedCombat(NetworkEntity attackee, float distanceBetweenCombatantsSquared, float damage)Raises CausedCombat. Called by health and combat code when this player deals damage.
override void OnConnected(Player player)Server: sends the owning player's id to a client who just loaded the body, so it can link them.
void OnPlayerLeaving()Server: for each hand, destroys a held item that saves with the player, otherwise drops it.
void OnPrePlayerLeaving()Server: raises OnAboutToLeave before the hands are cleared.
virtual void SaveTo(PlayerSave save)Hook to write into a PlayerSave. The base sets the chunk versioning.
virtual void UnlinkFromPlayer()Clears NetworkPlayer and, for the local player, Current.
void WaitForChunkLoad(Chunk previousChunk = null)Client: if the chunk the player is in isn't fully loaded, fades to black and marks the player not loaded until it is.
void WaitForChunkLoad(Chunk currentChunk, Chunk previousChunk)Client: if the chunk the player is in isn't fully loaded, fades to black and marks the player not loaded until it is.
protected virtual void Awake()Creates Info, applies the controller's save mode to its prefab and sets up the camera (culling only on non-headless builds).
protected virtual void DelayReposition()Called a frame after the local player links. Moves the body to its own position by feet to settle it.
protected virtual void LoadForChunk(Chunk currentChunk)Hook run when the local player starts waiting on a chunk. Empty in the base class.
protected virtual void OnDestroy()Clears Current if it was this, records the player as a recent player and unhooks settings.
protected void SaveNetworkPrefabToPlayerSave(PlayerSave save, bool isBeingDestroyed = false)Writes the body's position, prefab data and held items into a PlayerSave.
protected void SerializeHands(Stream stream, ChunkVersioning chunkVersioning)Saves or loads what each hand holds. On load it clears anything left on the hands, then spawns and grabs the saved items.
6 private and internal methods
MemberWhat it is
private void IBuildRendererBakeable.Clean()Explicit IBuildRendererBakeable build step. Does nothing.
private void ResetStandardVisuals()Client: resets post processing and returns the atmosphere to its base before waiting on a chunk load.
private bool SerializeInteractor(Stream stream, ChunkVersioning chunkVersioning, Interactor interactor, Interactor otherInteractor)Saves or loads the item held in one hand. On load it spawns the item and makes the hand grab it, destroying it if that fails.
private void SyncPlayer(IPlayer player, Stream stream)Synced message with the owning Player entity ID. Remote clients use it to link this controller to that player.
private void TriggeredFadeIn(Chunk chunk)Client: when the awaited chunk finishes loading, marks the player loaded and lifts the screen fade.
private void UpdateCameraCulling(GameSettings settings)Client: sets the camera's layer cull distance from the game settings' default culling distance. Rerun when settings change.

Used in​

Members of other types that take, return or hold a PlayerController:

ActionOrb.controller, AfkTimer.PlayerControllerSet, AfkTimer.PositionChanged, TimberBlockSpawner.CheckRelease, AccessibleDocks.InitializeAsLocalPlayer, AccessibleDocks.playerController, CharacterComponentController.playerController, CharacterComponentController.PlayerController, CustomControllerOffsetReader.OnLocalPlayerSet, PersonalSpace.character, PersonalSpace.Character, PlayerControllerHandler.BeginInvoke, PlayerControllerHandler.Invoke, PlayerText.DeleteIfLocalPlayer, PositionChangedHandler.BeginInvoke, PositionChangedHandler.Invoke, RotationChangedHandler.BeginInvoke, RotationChangedHandler.Invoke, ScaleChangedHandler.BeginInvoke, ScaleChangedHandler.Invoke, OcclusionChunkEffectData.DisableForPlayer, CraftingOptions.CraftingOptions, CraftingOptions.Create, CraftingOptions.player, JointInsert.linesPlayer, JointInsert.ShowLines, AtmosphereManager.OverrideAtmosphere, BorderDefinedAtmosphere.OnLocalPlayerSet, RiverAudio.LocalPlayerSet, RiverAudio.player, HandShakeOffDetector.UpdatePosition, ImpactDetector.InitializeAsLocalPlayer, ImpactDetector.InitializeAsRemotePlayer, ImpactDetector.IsImpactClientSideOnly, ImpactHit.ImpactHit, ImpactHit.PlayerWielder, ImpactTool.GetCurrentPlayerWielder, ImpactTool.PlayerWielder, PlayerCharacterTarget.character, PlayerCharacterTarget.Character, Chest.SetLocalPlayer, Chest.UpdateDistance, PickupDock.InitializeAsLocalPlayer, PickupDock.InitializeAsRemotePlayer, PickupDock.PlayerController, PickupDock.UpdatePlayerOwnership, PlayerDocks.owner, StabilizePickupJoint.MoveWithPlayer, StabilizePickupJoint.RecordLocalPositionBeforeMoving, PlayerLandmark.playerController, NetworkEntity.InitializeAsPlayer, NetworkEntityComponentController.InitializeAsLocalPlayer, NetworkEntityComponentController.InitializeAsRemotePlayer, NetworkTransform.InitializeAsLocalPlayer, PlayerComponentController.InitializeAsLocalPlayer, IPlayer.PlayerController, VoiceChatMouthController.controller, GlobalPagesManager.DissolvePage, PaperPageInstance.AutoAdd, DownedState.OnPlayerControllerSetRemote and 173 more.