From 18f080820e0f19d0e237b4d4d2f092ecb7e3b7c7 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 30 Dec 2024 15:45:06 -0500 Subject: [PATCH] feat(NetworkTranformBase): Provide velocity and angular velocity (#3909) * feat(NetworkTranformBase): Provide velocity and angular velocity * Update NetworkTransformBase.cs --------- Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com> --- .../NetworkTransform/NetworkTransformBase.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 85428ebf584..035c25f8741 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -119,6 +119,11 @@ public uint sendIntervalMultiplier protected double timeStampAdjustment => NetworkServer.sendInterval * (sendIntervalMultiplier - 1); protected double offset => timelineOffset ? NetworkServer.sendInterval * sendIntervalMultiplier : 0; + // velocity for covenience (animators etc.) + // this isn't technically NetworkTransforms job, but it's needed by so many projects that we just provide it anyway. + public Vector3 velocity { get; private set; } + public Vector3 angularVelocity { get; private set; } + // debugging /////////////////////////////////////////////////////////// [Header("Debug")] public bool showGizmos; @@ -263,6 +268,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 && Time.deltaTime > 0) + { + velocity = (transform.position - interpolated.position) / Time.deltaTime; + angularVelocity = (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);