Class CharacterHitReaction
A short, purely cosmetic displacement of a character's model, played the moment a hit is predicted rather than when the server's correction arrives.
public class CharacterHitReaction : MonoBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourCharacterHitReaction
- Inherited Members
-
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.MemberwiseClone()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.ToString()Object.nameObject.hideFlags
Remarks
Why not just predict the knockback. A knockback moves the target, and on the attacker's
client that target is a peer driven by NetworkTransform. Displacing it locally is
overwritten by the next transform update — one to three ticks away at the streaming LOD band
— so the character snaps back, which reads worse than the delay it was meant to hide. The
authoritative position cannot be predicted here and should not be.
What this does instead. It offsets MeshRoot, a child of the networked root that nothing else writes. The root keeps receiving the server's position untouched; the model leans off it and decays back to zero over ReactionSeconds. By the time the server's real displacement arrives the offset has largely resolved, so the two compose instead of fighting — the player sees the reaction on the frame they hit, and the translation catching up half a round trip later is invisible.
Cosmetic, and only cosmetic. Nothing here is read by the simulation, by hit detection, or by lag compensation — all of which work from the root transform and the position history. A client that lies to itself with this changes nothing anybody else can observe.
Fields
MaximumOffset
Metres of lean at full strength.
[Tooltip("Maximum lean distance in metres at full strength.")]
[Min(0)]
public float MaximumOffset
Field Value
Remarks
Deliberately small. This is a flinch, not a simulation of the knockback — the real displacement is the server's, and a large offset here would visibly double the motion when the authoritative one arrives.
ReactionSeconds
How long a reaction takes to decay back to rest, in seconds.
[Tooltip("Seconds for a hit reaction to decay back to rest.")]
[Min(0.01)]
public float ReactionSeconds
Field Value
Remarks
Short enough to be gone before the server's own displacement lands at a typical round trip, so the two do not visibly add. Longer than a couple of frames, or it reads as a glitch rather than as an impact.
ShoveCatchUpSeconds
Seconds a predicted shove may wait, after its own slide has ended, for the server's real displacement to catch it up before the lead it still holds fades out. A round trip plus the transform stream's interpolation, with room to spare; a shove the server refused (an immortal target, the harm rule) fades over this instead of snapping.
[Tooltip("Seconds a predicted shove waits for the server's real displacement before any lead it still holds fades out.")]
[Min(0.05)]
public float ShoveCatchUpSeconds
Field Value
Properties
IsPlaying
True while a reaction is still decaying.
public bool IsPlaying { get; }
Property Value
Methods
Play(Vector3, float)
Starts a reaction in a world-space direction.
public void Play(Vector3 worldDirection, float strength = 1)
Parameters
worldDirectionVector3Direction of the impact. Normalised internally; zero is ignored.
strengthfloat0 to 1, scaling MaximumOffset.
Remarks
Restarts rather than accumulates: a burst of hits produces one lean at the newest direction, not a model that walks away from its own root. Accumulating would let a multi-hit ability push the mesh arbitrarily far from the character it belongs to.
Silently does nothing without a mesh root — an NPC or a character whose model has not loaded has nothing to lean.
PlayOn(ICharacter, Vector3, float)
Plays a reaction on a character, adding the component the first time it is needed.
public static void PlayOn(ICharacter character, Vector3 worldDirection, float strength = 1)
Parameters
characterICharacterThe character being hit.
worldDirectionVector3Direction of the impact.
strengthfloat0 to 1.
Remarks
Added on demand rather than authored on prefabs: no character prefab carried it, so the predicted flinch never played anywhere. Only a client that plays a predicted reaction ever reaches this, so a server never adds one.
ResetToRest()
Cancels any reaction and returns the model to rest immediately.
public void ResetToRest()
Remarks
Public so a pooling or teardown path can call it directly rather than relying on
OnDisable firing — the object may be recycled without ever being disabled.
Shove(Vector3, float, float)
Starts a predicted SHOVE: the model slides along worldDirection as the server's knockback will move
the character, starting now rather than a round trip from now.
public void Shove(Vector3 worldDirection, float speed, float deceleration)
Parameters
worldDirectionVector3The push's direction; only its horizontal part is used.
speedfloatThe push, metres per second.
decelerationfloatHow fast it bleeds off, metres per second per second.
Remarks
The slide follows the same law the server applies (speed bled off at
deceleration), measured from where the networked root stood when it started. Each frame the model
is offset only by the part of that slide the ROOT has not yet made: as the server's real displacement arrives through
the transform stream the offset shrinks to nothing, so the two hand over without a jump and never add up. Whatever
lead is left when the slide is over fades out over ShoveCatchUpSeconds.
This is what the knockback's displacement could not be on the attacker's client (the root belongs to the server), done where it can be: on the model, which nothing networked reads.
ShoveOn(ICharacter, Vector3, float, float)
Plays a predicted shove (Shove(Vector3, float, float)) on a character, adding the component when needed.
public static void ShoveOn(ICharacter character, Vector3 worldDirection, float speed, float deceleration)
Parameters
characterICharacterworldDirectionVector3speedfloatdecelerationfloat