Skip to content

Instantly share code, notes, and snippets.

@Doprez
Created April 30, 2023 16:01
Show Gist options
  • Save Doprez/e146f8d30b7e9cea05de138d4e3a30f0 to your computer and use it in GitHub Desktop.
Save Doprez/e146f8d30b7e9cea05de138d4e3a30f0 to your computer and use it in GitHub Desktop.
I use this to fix an issue where if a camera is attached to a physics component it creates an annoying stutter when moving.
[DataContract(nameof(SmoothFollow))]
[ComponentCategory("Animation")]
public class SmoothFollow : SyncScript
{
public Entity EntityToFollow { get; set; }
public Vector3 Speed { get; set; } = new Vector3(1, 1, 1);
public override void Update()
{
var currentPosition = Entity.Transform.Position;
var otherPosition = EntityToFollow.WorldPosition;
var x = MathUtil.Lerp(currentPosition.X, otherPosition.X, Speed.X * DeltaTime);
var y = MathUtil.Lerp(currentPosition.Y, otherPosition.Y, Speed.Y * DeltaTime);
var z = MathUtil.Lerp(currentPosition.Z, otherPosition.Z, Speed.Z * DeltaTime);
Entity.Transform.Position = new Vector3(x, y, z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment