Created
April 7, 2024 21:54
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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