A simple, flexible C# solution for interpolating transforms in Godot 4.1 / 4.2.
Installation:
- Clone repo into your Godot C# project. Build your project.
- Go to Project/Project Settings/Autoload and add Interpolation.cs. Make sure it is enabled.
- Disable physics jitter fix in your project settings.
- (Reccommended) Disable vsync in your project settings.
Usage:
The asset works by using Engine.GetPhysicsInterpolationFraction()
to interpolate a node's position and rotation to that of its parent.
To enable interpolation on a node, simply call the static method Interpolation.Add(node)
from anywhere you like.
The method returns an InterpolationConfig
object which can be used to further control how the node gets interpolated.
By default, both position and rotation will be interpolated. You can change this by overriding the default parameters when adding a node:
Interpolation.Add(Node3D node, bool interpolatePosition = true, bool interpolateRotation = true, bool discardNonInterpolatedProperties = false)
InterpolationConfig.DiscardNonInterpolatedProperties
will determine if the non-interpolated properties of a node will inherit those of its parent.
You can set InterpolationConfig.DisableNextFrame = true
to disable interpolation for one frame. It will be automatically set to false at the end of the frame.
You can call the static method Interpolation.Get(Node3D node)
to retrieve its InterpolationConfig
object should it have one.
You can remove interpolation from a node by calling the static method Interpolation.Remove(Node3D node)
A basic example script BasicInterpolator.cs
has been provided for demonstration. You can attach it directly to a node (such as the MeshInstance3D of a rigidbody) to test if interpolation is working, and see how different settings affect it. You can delete this file if you don't need it.
Obviously, you should not interpolate the position of any physics nodes directly. The point of interpolation is to separate the visual representation of an entity with its physical representation. You can and should also use this asset to interpolate your Camera's position. You could also use it to interpolate it's rotation, but you probably don't want to. For responsive mouse movement, set Input.UseAccumulatedInput
to false
and update your camera's rotation directly from _Input()
or _UnhandledInput()
. Or, if your camera is a child of another node which (smoothly) controls its rotation, just disable InterpolationConfig.InterpolateRotation
and make sure InterpolationConfig.DiscardNonInterpolatedProperties
is also set to false