From 2508cfd618e9ab16b3faf688c926cef69b01c3d9 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 30 Sep 2024 08:41:02 -0400 Subject: [PATCH] feat(NetworkTranformBase): Provide velocity and angular velocity --- .../NetworkTransform/NetworkTransformBase.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 85428ebf5..15e33d65e 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -119,6 +119,23 @@ public uint sendIntervalMultiplier protected double timeStampAdjustment => NetworkServer.sendInterval * (sendIntervalMultiplier - 1); protected double offset => timelineOffset ? NetworkServer.sendInterval * sendIntervalMultiplier : 0; + [Header("Velocity")] + [ReadOnly, SerializeField] Vector3 _velocity; + [ReadOnly, SerializeField] Vector3 _angVelocity; + + public Vector3 velocity + { + get => _velocity; + internal set => _velocity = value; + } + + public Vector3 angVelocity + + { + get => _angVelocity; + internal set => _angVelocity = value; + } + // debugging /////////////////////////////////////////////////////////// [Header("Debug")] public bool showGizmos; @@ -263,6 +280,14 @@ protected virtual void Apply(TransformSnapshot interpolated, TransformSnapshot e // -> but simply don't apply it. if the user doesn't want to sync // scale, then we should not touch scale etc. + // Calculate the velocity and angular velocity for the object + // These can be used to drive animations or other behaviours + if (!isOwned) + { + velocity = (transform.position - interpolated.position) / Time.deltaTime; + angVelocity = (transform.rotation.eulerAngles - interpolated.rotation.eulerAngles) / Time.deltaTime; + } + // interpolate parts if (syncPosition) SetPosition(interpolatePosition ? interpolated.position : endGoal.position); if (syncRotation) SetRotation(interpolateRotation ? interpolated.rotation : endGoal.rotation);