Skip to content

Instantly share code, notes, and snippets.

@Doprez
Created April 7, 2024 21:54
Show Gist options
  • Save Doprez/903961a5426b1b86af3e43eaec0890f1 to your computer and use it in GitHub Desktop.
Save Doprez/903961a5426b1b86af3e43eaec0890f1 to your computer and use it in GitHub Desktop.
Fixes jitter found in the default templates due to the camera being directly attached to a physics component.
[ComponentCategory("Utils")]
[DataContract("SmoothFollowAndRotate")]
public class SmoothFollowAndRotate : SyncScript
{
public Entity EntityToFollow { get; set; }
public float Speed { get; set; } = 1;
public override void Update()
{
var deltaTime = (float)Game.UpdateTime.Elapsed.TotalSeconds;
var currentPosition = Entity.Transform.Position;
var currentRotation = Entity.Transform.Rotation;
var lerpSpeed = 1f - MathF.Exp(-Speed * deltaTime);
EntityToFollow.Transform.GetWorldTransformation(out var otherPosition, out var otherRotation, out var _);
var newPosition = Vector3.Lerp(currentPosition, otherPosition, lerpSpeed);
Entity.Transform.Position = newPosition;
Quaternion.Slerp(ref currentRotation, ref otherRotation, lerpSpeed, out var newRotation);
Entity.Transform.Rotation = newRotation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment