Class TargetController
Controls targeting logic for a character, including raycasting, target selection, and target events.
public class TargetController : CharacterBehaviour, ITargetController, ICharacterBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourNetworkBehaviourTargetController
- Implements
- Inherited Members
-
NetworkBehaviour.IsSpawnedNetworkBehaviour.ComponentIndexNetworkBehaviour.NetworkObjectNetworkBehaviour.MAXIMUM_NETWORKBEHAVIOURSNetworkBehaviour.UNSET_NETWORKBEHAVIOUR_IDNetworkBehaviour.ToString()NetworkBehaviour.Reset()NetworkBehaviour.OnValidate()NetworkBehaviour.IsBehaviourReconcilingNetworkBehaviour.ClearReplicateCache()NetworkBehaviour.CreateReconcile()NetworkBehaviour.Reconcile_Reader<T>(PooledReader, ref T)NetworkBehaviour.OnStartServerCalledNetworkBehaviour.OnStartClientCalledNetworkBehaviour.WritePayload(NetworkConnection, Writer)NetworkBehaviour.ReadPayload(NetworkConnection, Reader)NetworkBehaviour.OnStartNetwork()NetworkBehaviour.OnStopNetwork()NetworkBehaviour.OnStartServer()NetworkBehaviour.OnStopServer()NetworkBehaviour.OnOwnershipServer(NetworkConnection)NetworkBehaviour.OnSpawnServer(NetworkConnection)NetworkBehaviour.OnDespawnServer(NetworkConnection)NetworkBehaviour.OnStartClient()NetworkBehaviour.OnStopClient()NetworkBehaviour.OnOwnershipClient(NetworkConnection)NetworkBehaviour.ClearBuffedRpcs()NetworkBehaviour.ExcludeOwnerFromUnbufferedObserversRpcsNetworkBehaviour.IsClientOnlyNetworkBehaviour.IsServerOnlyNetworkBehaviour.IsHostNetworkBehaviour.IsClientNetworkBehaviour.IsServerNetworkBehaviour.IsDeinitializingNetworkBehaviour.NetworkManagerNetworkBehaviour.ServerManagerNetworkBehaviour.ClientManagerNetworkBehaviour.ObserverManagerNetworkBehaviour.TransportManagerNetworkBehaviour.TimeManagerNetworkBehaviour.SceneManagerNetworkBehaviour.PredictionManagerNetworkBehaviour.RollbackManagerNetworkBehaviour.NetworkObserverNetworkBehaviour.IsClientInitializedNetworkBehaviour.IsClientStartedNetworkBehaviour.IsClientOnlyInitializedNetworkBehaviour.IsClientOnlyStartedNetworkBehaviour.IsServerInitializedNetworkBehaviour.IsServerStartedNetworkBehaviour.IsServerOnlyInitializedNetworkBehaviour.IsServerOnlyStartedNetworkBehaviour.IsHostInitializedNetworkBehaviour.IsHostStartedNetworkBehaviour.IsOfflineNetworkBehaviour.IsNetworkedNetworkBehaviour.GetIsNetworked()NetworkBehaviour.IsManagerReconcilingNetworkBehaviour.ObserversNetworkBehaviour.IsOwnerNetworkBehaviour.IsControllerNetworkBehaviour.HasAuthorityNetworkBehaviour.OwnerNetworkBehaviour.OwnerIdNetworkBehaviour.ObjectIdNetworkBehaviour.LocalConnectionNetworkBehaviour.OwnerMatches(NetworkConnection)NetworkBehaviour.Despawn(GameObject, DespawnType?)NetworkBehaviour.Despawn(NetworkObject, DespawnType?)NetworkBehaviour.Despawn(DespawnType?)NetworkBehaviour.Spawn(GameObject, NetworkConnection, Scene)NetworkBehaviour.Spawn(NetworkObject, NetworkConnection, Scene)NetworkBehaviour.RemoveOwnership()NetworkBehaviour.GiveOwnership(NetworkConnection)NetworkBehaviour.GetInstance<T>()NetworkBehaviour.TryRegisterInstance<T>(T)NetworkBehaviour.UnregisterInstance<T>()NetworkBehaviour.CanLog(LoggingType)MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.nameObject.hideFlags
Fields
Last
The previous target information.
public TargetInfo Last
Field Value
LayerMask
The layer mask used for target raycasts.
public LayerMask LayerMask
Field Value
- LayerMask
MAX_TARGET_DISTANCE
The maximum distance allowed for targeting.
public const float MAX_TARGET_DISTANCE = 50
Field Value
TARGET_UPDATE_RATE
The update rate (in seconds) for target checks.
public const float TARGET_UPDATE_RATE = 0.05
Field Value
Properties
ClientSelectedTargetObjectId
SERVER-side view of the owning client's reported target frame:
the NetworkObject id the player is looking at, or 0. Advisory — feeds interest
management, never combat resolution. See TargetSelectionBroadcast.
public int ClientSelectedTargetObjectId { get; }
Property Value
Current
The current target information.
public TargetInfo Current { get; }
Property Value
HasClientSelectedTarget
True once any client target report has been accepted for this character. Distinguishes "the player reports no target" (authoritative 0) from "this character has no reporting client at all" (an NPC, or an old client), where readers fall back to the cast-scoped Current.
public bool HasClientSelectedTarget { get; }
Property Value
OnTargetChangeTriggers
Triggers invoked when the target changes to a new target.
public List<Trigger> OnTargetChangeTriggers { get; }
Property Value
OnTargetClearTriggers
Triggers invoked when the current target is cleared.
public List<Trigger> OnTargetClearTriggers { get; }
Property Value
PinnedTarget
The character the owning client has pinned to its target frame, or null.
public Transform PinnedTarget { get; }
Property Value
- Transform
Remarks
A pin is a HUD concept: the pinned card stays up while the pointer wanders, so the player can follow one opponent through a fight. It is never a combat target — ability acquisition stays a server-side raycast from the replicated aim — and it lives on the owning client only. On the server this is always null.
Methods
ClearPinnedTarget()
Releases the pinned target, if any.
public void ClearPinnedTarget()
InitializeOnce()
Called after registration for custom initialization logic. Override in derived classes.
public override void InitializeOnce()
OnDestroying()
Called when the object is being destroyed. Clears target events and resets state.
public override void OnDestroying()
ResetState(bool)
Clears client-reported target state on despawn/pool reuse: the next occupant of a pooled object must not inherit the previous player's target frame.
public override void ResetState(bool asServer)
Parameters
asServerbool
ServerSetClientSelectedTarget(int)
Installs a VERIFIED client target report. Server only; callers must have validated the id resolves to a live character in the reporting client's own scene (or pass 0).
public void ServerSetClientSelectedTarget(int targetObjectId)
Parameters
targetObjectIdint
TogglePinnedTarget()
Pins the hovered character, or releases the pin when nothing — or the pinned character itself — is under the pointer. Owning client only.
public bool TogglePinnedTarget()
Returns
- bool
True when a target is pinned after the call.
TryPinTarget(Transform)
Pins a specific character. Refused for non-characters, unspawned objects and the player's own character. Owning client only.
public bool TryPinTarget(Transform target)
Parameters
targetTransformThe transform to pin.
Returns
- bool
True when the target is pinned after the call.
UpdateTarget(Vector3, Vector3, float)
Updates and returns the TargetInfo for the current target, tracing from the given origin and direction.
public TargetInfo UpdateTarget(Vector3 origin, Vector3 direction, float maxDistance)
Parameters
originVector3The origin of the ray.
directionVector3The direction of the ray.
maxDistancefloatThe maximum distance for the raycast.
Returns
- TargetInfo
The updated TargetInfo.
Remarks
Lag compensated on the server. This is the acquisition step for every targeted
ability: AbilityController.ResolveTargetAndSpawn calls it from the replicated aim,
and TargetedEntitySelector then resolves the ability onto whatever it
decided. A ray is infinitely thin, so it has no width to absorb the gap between where a
client renders its peers and where the server holds them — measured at 0.64 m for a
40 ms connection and 2.2 m at 300 ms, against character capsules 0.6 m
across. Uncompensated, a laggy player's ray simply misses the character they aimed at.
Rewinding the scene for the caster's view puts the ray back where it was pointed.
One physics scene, both peers. The server branch used to be behind
#if UNITY_SERVER, which is undefined in the editor the scene server is developed
in — so in that configuration the server took the client branch and traced the
default physics scene, which for a multi-scene server holds none of the colliders
around the character. Asking the character which scene it is in answers correctly on
every peer and in every build, and it is what makes the owner and the server trace the
same geometry. The aim itself already matches: KCCPlayer quantises the direction
through AimDirectionCompression before it enters the replicate, so the owner
simulates with the value the server will receive rather than with its raw camera.
Events
OnChangeTarget
Event triggered when the target changes.
public event Action<Transform> OnChangeTarget
Event Type
- Action<Transform>
OnClearTarget
Event triggered when the target is cleared (e.g., deselected).
public event Action<Transform> OnClearTarget
Event Type
- Action<Transform>
OnPinTarget
Event triggered on the owning client when a target is pinned.
public event Action<Transform> OnPinTarget
Event Type
- Action<Transform>
OnUnpinTarget
Event triggered on the owning client when the pinned target is released. The argument is the released transform, or null when it was destroyed out from under the pin.
public event Action<Transform> OnUnpinTarget
Event Type
- Action<Transform>
OnUpdateTarget
Event triggered when the target is updated (but not changed).
public event Action<Transform> OnUpdateTarget
Event Type
- Action<Transform>