Photon Fusion 1.1.2

Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Properties | List of all members
NetworkCharacterController Class Reference

Provides custom API and state replication for collider-based character controller movement (not related to Unity's CharacterController type). Replicates both the internal state (Velocity, MaxSpeed, etc) and the Unity Transform data from the NetworkObject.StateAuthority to all other peers. Add this component to a GameObject to control movement and sync the position and/or rotation accurately, including client-side prediction. More...

Inherits NetworkTransformObsolete.

Classes

class  Configuration
 Configuration data used both by ComputeRawMovement and the prototype-grade Move and ComputeRawSteering methods. More...
 
struct  Hit
 Hit data passed as parameters to NetworkCharacterController.ICallbacks methods. More...
 
interface  ICallbacks
 Collision and trigger callback interface that can optionally be passed to the movement query. Common uses: bypass a contact, apply a contact force to the target (can also be achieved just by adding a kinematic Rigidbody to the Character Controller. More...
 
struct  Movement
 Result of a movement query. More...
 

Public Types

enum class  BumpBehavior
 What to do with collisions that are considered a bump. More...
 
enum class  MovementType
 Movement type computed by the movement query based on collisions and surface tangents. More...
 
- Public Types inherited from NetworkBehaviour
enum class  InterpolationDataSources
 Options for which time frame this object will render in. More...
 

Public Member Functions

override void Spawned ()
 Post spawn callback. More...
 
void Jump (bool ignoreGrounded=false, float? impulse=null)
 Basic implementation of a jump impulse (immediately integrates a vertical component to Velocity).

Parameters
ignoreGroundedJump even if not in a grounded state.
impulseOptional, if null Config.BaseImpulse will be used.
More...
 
void Move (Vector3 direction, ICallbacks callback=null, LayerMask? layerMask=null)
 Basic implementation of a full Move. Performs a movement query, uses its result to compute new Velocity, then applies penetration corrections + velocity integration into the transform position. Does not change Rotation.

Parameters
directionIntended movement direction, subject to movement query + acceleration.
callbackOptional custom callbacks object.
layerMaskOptional layermask. If not passed, the default one from Config will be used
More...
 
Movement ComputeRawMovement (Vector3 direction, ICallbacks callback=null, LayerMask? layerMask=null)
 Static query for surface movement data. Does not modify any data on state (neither transform, nor self).

Parameters
directionIntended movement direction. WIll be used to compute surface tangents.
callbackOptional custom callbacks object.
layerMaskOptional layermask. If not passed, the default one from Config will be used
Returns
The NetworkCharacterController.Movement results
More...
 
- Public Member Functions inherited from NetworkTransformObsolete
override void Spawned ()
 Post spawn callback. More...
 
override void CopyBackingFieldsToState (bool firstTime)
 
void AfterTick ()
 Called after each tick simulation completes. More...
 
void Teleport (Vector3? position, Rotation? rotation=null, Vector3? localScale=null, bool reset=false, Vector3? velocity=null, Vector3? angularVelocity=null)
 Teleport indicates that objects should not interpolate from the previous state to this new state. More...
 
void Teleport (NetworkTransformAnchor newParent, Vector3? position, Rotation? rotation=null, Vector3? localScale=null, bool reset=false, Vector3? velocity=null, Vector3? angularVelocity=null)
 
void PreTeleport ()
 Captures the current state as the interpolation lerp TO target leading up to a teleport, and flags the next tick as a teleport. Call this BEFORE making any TRS changes to the synced transform which represent the teleport. For continuously moving rigidbodies, it is preferable to call the explicit Teleport() method. More...
 
override void Render ()
 Post simulation frame rendering callback. Runs after all simulations have finished. Use in place of Unity's Update when Fusion is handling Physics. More...
 
- Public Member Functions inherited from NetworkTransformAnchor
virtual void RemotePrefabCreated ()
 
virtual void BeforeAllTicks (bool resimulation, int tickCount)
 Called before the resimulation loop (when applicable), and also before the forward simulation loop. Only called on Updates where resimulation or forward ticks are processed. More...
 
virtual void AfterAllTicks (bool resimulation, int tickCount)
 Called after the resimulation loop (when applicable), and also after the forward simulation loop. Only called on Updates where resimulation or forward ticks are processed. More...
 
virtual void BeforeCopyPreviousState ()
 
- Public Member Functions inherited from NetworkBehaviour
delegate int[] InterestGroupsCallback (Type type, NetworkBehaviour behaviour)
 
int GetLocalAuthorityMask ()
 Gets a bitmask of AuthorityMasks flags, representing the current local authority over this NetworkObject. More...
 
void CopyStateFrom (NetworkBehaviour source)
 Copies entire state of passed in source NetworkBehaviour More...
 
override void FixedUpdateNetwork ()
 Fusion FixedUpdate timing callback. More...
 
virtual void CopyStateToBackingFields ()
 
virtual void Despawned (NetworkRunner runner, bool hasState)
 Called before the network object is despawned More...
 
bool GetInterpolationData (out InterpolationData data, bool? predicted=null)
 Get 'To' and 'From' states, and the Alpha (t) values needed for lerping. More...
 
bool GetInterpolationData (out InterpolationData data, out bool predicted)
 Get 'To' and 'From' states, and the Alpha (t) values needed for interpolation. More...
 
virtual bool IsInterpolationDataPredicted ()
 If the behaviour data should be interpolated between latest predicted states or between snapshots. More...
 
void OnChangeClearAll ()
 Clear all dynamic OnChange callbacks from this behaviour More...
 
bool OnChangeRemove (NetworkBehaviourCallbackReference reference)
 Removes a specific OnChange callback from this behaviour More...
 
NetworkBehaviourCallbackReference OnChangeAdd< T > (int wordOffset, int wordCount, ChangedDelegate< T > callback, OnChangedTargets targets=OnChangedTargets.All)
 Adds a OnChange callback to this behaviour which is bound to a specific word offset and word count More...
 
NetworkBehaviourCallbackReference OnChangeAdd< T > (string propertyName, ChangedDelegate< T > callback, OnChangedTargets targetses=OnChangedTargets.All)
 Adds a OnChange callback to this behaviour which is bound to a specific weaved property More...
 
T? GetInput< T > ()
 
bool GetInput< T > (out T input)
 Returns true if it a valid INetworkInput can be found for the current simulation tick (Typically this is used in FixedUpdateNetwork). More...
 
RawInterpolator GetInterpolator (string propertyName)
 Get a raw interpolator for a networked property. The returned RawInterpolator provides a way to calculate the "between-ticks" value of the named [Networked] property. More...
 
Interpolator< T > GetInterpolator< T > (string propertyName)
 Get an interpolator for a networked property. The returned Interpolator provides a way to calculate the "between-ticks" value of the named [Networked] property with the specified type T. The value is a linear interpolation between the "from" and the "to" ticks and is available via the Value property on the Interpolator. More...
 
- Public Member Functions inherited from Behaviour
AddBehaviour< T > ()
 Wrapper for Unity's GameObject.AddComponent() More...
 
bool TryGetBehaviour< T > (out T behaviour)
 Wrapper for Unity's GameObject.TryGetComponent() More...
 
GetBehaviour< T > ()
 Wrapper for Unity's GameObject.GetComponentInChildren() More...
 

Public Attributes

Configuration Config
 NetworkCharacterController.Configuration. More...
 
- Public Attributes inherited from NetworkTransformObsolete
bool InterpolateErrorCorrection = true
 
InterpolatedErrorCorrectionSettings InterpolatedErrorCorrectionSettings
 
- Public Attributes inherited from NetworkBehaviour
int * Ptr
 Pointer to the allocated memory associated with this Object. More...
 
int ObjectIndex
 The index of this NetworkBehaviour, in the NetworkObject.NetworkedBehaviours array. More...
 
bool InvokeRpc
 
int offset
 Gives access to the offset (in 32 bit words) and count (in 32 bit words) of this behaviour backing data More...
 
- Public Attributes inherited from SimulationBehaviour
NetworkRunner Runner
 The NetworkRunner this component is associated with. More...
 
NetworkObject Object
 The NetworkObject this component is associated with. May be null if this GameObject does not have a NetworkObject. More...
 

Protected Member Functions

override void Awake ()
 
override void Reset ()
 
override void ApplyQueuedTeleport ()
 Applies the latest unconsumed Teleport() call after the Forward simulation is complete, and before the TRS state is captured. More...
 
- Protected Member Functions inherited from NetworkTransformObsolete
override void Awake ()
 
virtual void GetEnginePositionRotation2Buffer (out Vector3 pos, out Quaternion rot)
 
virtual void Copy2BufferTRSState (int offset)
 
virtual void Copy2EngineTRSState (int offset)
 
virtual Quaternion ReadRotationToQuaternion (int *offset, ReadAccuracy readAccuracy)
 
void InterpolateTransform (ref InterpolationData data, bool isState)
 
virtual void ApplyTransform (ref UpdateTransformParameters param)
 
- Protected Member Functions inherited from NetworkTransformAnchor
virtual void OnEnable ()
 
bool Copy2EngineAnchorState ()
 
void Copy2BufferAnchorState (int offset=PARENT_OFFSET)
 

Properties

override int PositionWordOffset [get]
 Internal use for automatic Area-of-Interest management. More...
 
bool Grounded [get, set]
 Networked current grounded state (used only by Move and COmputeRawSteering) More...
 
bool Jumped [get, set]
 Networked current jumped state (used only by Move and ComputeRawSteering to avoid clamping a jump speed when on the first subsequent query the collider still touches the ground) More...
 
float MaxSpeed [get, set]
 Networked current max speed (used only by Move and ComputeRawSteering to clamp horizontal components of Velocity) More...
 
Vector3 Velocity [get, set]
 Networked current velocity (used only by Move and ComputeRawSteering to move the Character Controller) More...
 
- Properties inherited from NetworkTransformObsolete
override int PositionWordOffset [get]
 Gets the Ptr offset of the Position values in allocated memory. More...
 
int * TeleportCounter [get]
 The frame of the next Teleport action. More...
 
- Properties inherited from NetworkTransformAnchor
override int PositionWordOffset [get]
 Gets the Ptr offset of the Position values in allocated memory. More...
 
Transform InterpolationTarget [get, set]
 The Transform object used for interpolation. Should be a non-physics GameObject. Typically a child of this GameObject without colliders, or a separate GameObject without colliders. More...
 
Transform Transform [get]
 
- Properties inherited from NetworkAreaOfInterestBehaviour
abstract int PositionWordOffset [get]
 The int* offset for the Ptr, for the memory location of the position data. More...
 
- Properties inherited from NetworkBehaviour
InterpolationDataSources InterpolationDataSource [get, set]
 Get/Set the time frame this object is rendered in. More...
 
NetworkBehaviourId Id [get]
 The unique identifier for this network behaviour. More...
 
virtual ? int DynamicWordCount [get]
 Override this value for custom memory allocations. This is for advanced use cases only, and cannot be used if NetworkedAttribute is used in the derived class.
More...
 
virtual bool InvokeOnChangedForInitialNonZeroValues [get]
 Override this property to change whether initial non-zero values assigned to [Networked] properties invoke OnChanged callbacks. For clients the initial value comes from the simulation snapshot, not from what has been set locally in the inspector. More...
 
- Properties inherited from SimulationBehaviour
bool CanReceiveCallback [get]
 
virtual bool? HasInputAuthority [get]
 Returns true if the Simulation.LocalPlayer of the associated NetworkRunner is the designated as Input Authority for this network entity. More...
 
virtual bool? HasStateAuthority [get]
 Returns true if the associated NetworkRunner is the State Authority for this network entity. More...
 
virtual bool? IsProxy [get]
 Returns true if the associated NetworkRunner is neither the Input nor State Authority for this network entity. It is recommended to use !HasStateAuthority or !HasInputAuthority when possible instead, as this check requires evaluating both authorities - and is therefore less performant than the individual checks. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from NetworkBehaviour
static int NetworkSerialize (NetworkRunner runner, NetworkBehaviour obj, byte *data)
 
static int NetworkDeserialize (NetworkRunner runner, byte *data, ref NetworkBehaviour result)
 
static NetworkBehaviourId NetworkWrap (NetworkRunner runner, NetworkBehaviour obj)
 
static NetworkBehaviour NetworkUnwrap (NetworkRunner runner, NetworkBehaviourId wrapper)
 
static ref T MakeRef< T > ()
 
static ref T MakeRef< T > (T defaultValue)
 
static T * MakePtr< T > ()
 
static T * MakePtr< T > (T defaultValue)
 
static NetworkBehaviourUtils.ArrayInitializer< T > MakeInitializer< T > (T[] array)
 This is a special method that is meant to be used only for [Networked] properties inline initialization. More...
 
static NetworkBehaviourUtils.DictionaryInitializer< K, V > MakeInitializer< K, V > (Dictionary< K, V > dictionary)
 This is a special method that is meant to be used only for [Networked] properties inline initialization. More...
 
static implicit operator NetworkBehaviourId (NetworkBehaviour behaviour)
 Converts NetworkBehaviour to NetworkBehaviourId More...
 
- Static Public Member Functions inherited from Behaviour
static void DestroyBehaviour (Behaviour behaviour)
 Wrapper for Unity's GameObject.Destroy() More...
 
- Static Public Attributes inherited from NetworkTransformObsolete
const int POSITION_OFFSET = ANCHOR_WORD_COUNT - 3
 The Ptr offset location of the position values in allocated memory. More...
 
const int ROTATION_OFFSET = POSITION_OFFSET + 3
 The Ptr offset location of the rotation values in allocated memory. More...
 
const int SCALE_OFFSET = ROTATION_OFFSET + 4
 The Ptr offset location of the local scale values in allocated memory. More...
 
- Protected Attributes inherited from NetworkTransformObsolete
bool SyncScale
 Indicates if Scale should be included in Transform sync and interpolation (when enabled). More...
 
Spaces Space = Spaces.World
 Select if the synced state uses World or Local position and rotation values. More...
 
float LastInterpolatedAtTime
 Stores the last UnityEngine.Time.time when interpolation was run for this component. Used to ensure interpolation is only run once per screen refresh. More...
 
bool includeParent
 Most recent unconsumed Teleport() command. More...
 
- Protected Attributes inherited from NetworkTransformAnchor
Transform _interpolationTarget
 The Transform object used for interpolation. Should be a non-physics GameObject. Typically a child of this Rigidbody, or a separate object without colliders. The runner/simulation with State Authority will not interpolate if target is null or the same GameObject as the Rigidbody, as this will conflict with the PhysX simulation. More...
 
bool DetachInterpTarget
 EXPERIMENTAL: To improve interpolation of scaling, the interpolation target is detached from its NetworkObject. Only check this if you are syncing scaling. When re-parenting, this component's InterpolationTarget will re-parent to the parent's InterpolationTarget. More...
 
bool SyncParent = true
 Indicates if Transform.parent will be synced. Parent syncing requires the parent of this object either be null or have a NetworkTransformAnchor component. More...
 
- Static Protected Attributes inherited from NetworkTransformObsolete
const int TELE_POS_OFFSET = SCALE_OFFSET + 3
 The Ptr offset for the secondary teleport position information. This position is used as the lerp TO target leading up to the teleport. More...
 
const int TELE_ROT_OFFSET = TELE_POS_OFFSET + 3
 The Ptr offset for the secondary teleport rotation information. This rotation is used as the lerp TO target leading up to the teleport. More...
 
const int TELE_SCL_OFFSET = TELE_ROT_OFFSET + 4
 The Ptr offset for the secondary teleport scale information. This scale is used as the lerp TO target leading up to the teleport. More...
 
const int TELEPORT_OFFSET = TELE_SCL_OFFSET + 3
 The Ptr offset of the teleport increment value. Increases by one every tick a teleport occurs. More...
 
const int BASE_WORD_COUNT = TELEPORT_OFFSET + 1
 The total number of words used by NetworkTransformObsolete. Derived classes using allocated memory should start their Ptr offsets using this value. More...
 
- Static Protected Attributes inherited from NetworkTransformAnchor
const int PARENT_OFFSET = 0
 The Ptr offset for the memory position of the Parent info. The parenting info is 2 words (8 bytes). More...
 
const int TELE_PAR_OFFSET = PARENT_OFFSET + 2
 The Ptr offset for the secondary teleport parent information. This parent is used by the lerp TO target leading up to the teleport. More...
 
const int ANCHOR_WORD_COUNT = POSITION_OFFSET + 3
 The total number of words used by NetworkTransformAnchor. Derived classes using allocated memory should start their Ptr offsets using this value. More...
 

Detailed Description

Provides custom API and state replication for collider-based character controller movement (not related to Unity's CharacterController type). Replicates both the internal state (Velocity, MaxSpeed, etc) and the Unity Transform data from the NetworkObject.StateAuthority to all other peers. Add this component to a GameObject to control movement and sync the position and/or rotation accurately, including client-side prediction.

Usage - For basic prototyping call the following methods:

NetworkCharacterController.Move()
NetworkCharacterController.Jump()

For more advanced uses, call only the movement query to get full surface movement data (NetworkCharacterController.Movement), and implement bespoke Move/Steering:

NetworkCharacterController.Jump()

A NetworkObject is required. Not to be combined with either NetworkTransformObsolete nor NetworkRigidbodyObsolete. These are mutually-exclusive options.

Member Enumeration Documentation

◆ BumpBehavior

enum BumpBehavior
strong

What to do with collisions that are considered a bump.

Enumerator
AlwaysContact 

Collision always affects this Character Controller, no matter the movement situation.

IgnoreOnStanding 

Automatically ignore collisions when character is standing. (the Other NetworkCharacterController will still be affected and not allowed penetrate)

IgnoreOnBackside 

(Not implemented yet!) Automatically ignore collisions when collision is from sides or back. (the Other NetworkCharacterController will still be affected and not allowed penetrate)

◆ MovementType

enum MovementType
strong

Movement type computed by the movement query based on collisions and surface tangents.

Enumerator
None 

No desired movement, and standing on a ground.

FreeFall 

No ground or slope collisions (there may be a collision on the upper part).

SlopeFall 

No collision qualifies as grounded (angle between up and normal is smaller or equals to max slope). At least one collision that qualifies as slope (angle between up and surface normal is larger than max slope).

Horizontal 

There's a desired movement direction. At least one collision qualifies as grounded (angle between up and normal is smaller or equals to max slope).

Member Function Documentation

◆ ApplyQueuedTeleport()

override void ApplyQueuedTeleport ( )
protectedvirtual

Applies the latest unconsumed Teleport() call after the Forward simulation is complete, and before the TRS state is captured.

Reimplemented from NetworkTransformObsolete.

◆ ComputeRawMovement()

Movement ComputeRawMovement ( Vector3  direction,
ICallbacks  callback = null,
LayerMask?  layerMask = null 
)

Static query for surface movement data. Does not modify any data on state (neither transform, nor self).

Parameters
directionIntended movement direction. WIll be used to compute surface tangents.
callbackOptional custom callbacks object.
layerMaskOptional layermask. If not passed, the default one from Config will be used
Returns
The NetworkCharacterController.Movement results

◆ Jump()

void Jump ( bool  ignoreGrounded = false,
float?  impulse = null 
)

Basic implementation of a jump impulse (immediately integrates a vertical component to Velocity).

Parameters
ignoreGroundedJump even if not in a grounded state.
impulseOptional, if null Config.BaseImpulse will be used.

◆ Move()

void Move ( Vector3  direction,
ICallbacks  callback = null,
LayerMask?  layerMask = null 
)

Basic implementation of a full Move. Performs a movement query, uses its result to compute new Velocity, then applies penetration corrections + velocity integration into the transform position. Does not change Rotation.

Parameters
directionIntended movement direction, subject to movement query + acceleration.
callbackOptional custom callbacks object.
layerMaskOptional layermask. If not passed, the default one from Config will be used

◆ Spawned()

override void Spawned ( )
virtual

Post spawn callback.

Reimplemented from NetworkBehaviour.

Member Data Documentation

◆ Config

Configuration Config

Property Documentation

◆ Grounded

bool Grounded
getset

Networked current grounded state (used only by Move and COmputeRawSteering)

◆ Jumped

bool Jumped
getset

Networked current jumped state (used only by Move and ComputeRawSteering to avoid clamping a jump speed when on the first subsequent query the collider still touches the ground)

◆ MaxSpeed

float MaxSpeed
getset

Networked current max speed (used only by Move and ComputeRawSteering to clamp horizontal components of Velocity)

◆ PositionWordOffset

override int PositionWordOffset
get

Internal use for automatic Area-of-Interest management.

◆ Velocity

Vector3 Velocity
getset

Networked current velocity (used only by Move and ComputeRawSteering to move the Character Controller)