//This will draw spheres at the positions the player has been at, to help visualize movement and speed.
//Each player will have a different random color for their position history, to help differentiate between players in multiplayer.
public sealed class PlayerPositionDebug : Component
{
private readonly Queue<Vector3> positionHistory = new();
[Property]
public bool ShowPositionHistory { get; set; } = true;
[Property]
public float RateOfPositionHistory { get; set; } = 0.5f;
[Property]
public int MaxPositionHistory { get; set; } = 7;
[Property]
public float SphereRadius { get; set; } = 5f;
private TimeSince timeSinceLastPosition;
private Color randomColor;
protected override void OnStart()
{
base.OnStart();
randomColor = Color.Random;
}
protected override void OnUpdate() { }
protected override void DrawGizmos()
{
base.DrawGizmos();
UpdatePositionHistory();
}
private void UpdatePositionHistory()
{
if (ShowPositionHistory)
{
if (timeSinceLastPosition >= RateOfPositionHistory)
{
timeSinceLastPosition = 0f;
positionHistory.Enqueue(GameObject.Transform.World.Position);
}
while (positionHistory.Count > MaxPositionHistory)
{
positionHistory.Dequeue();
}
}
else
{
positionHistory.Clear();
}
foreach (var position in positionHistory)
{
DrawLineSphere(position);
}
}
private void DrawLineSphere(Vector3 position)
{
using (Gizmo.Scope())
{
//Important to set the transform to zero, otherwise the spheres will be drawn relative to the player's position, which is not what we want.
Gizmo.Transform = global::Transform.Zero;
Gizmo.Draw.Color = randomColor;
Gizmo.Draw.LineSphere(position, SphereRadius);
}
}
}