A small game component that tracks an entity's velocity. It stores the last known world position and computes CurrentVelocity each fixed update by dividing displacement by Time.Delta.
using Sandbox;
public sealed class AnimatedVelocityComponent : Component
{
[Property] private Vector3 lastPosition;
[Property] public Vector3 CurrentVelocity { get; private set; }
protected override void OnStart()
{
lastPosition = WorldPosition;
}
protected override void OnFixedUpdate()
{
// Calculate velocity (units per second)
Vector3 displacement = WorldPosition - lastPosition;
CurrentVelocity = displacement / Time.Delta;
// Update the last position
lastPosition = WorldPosition;
}
}