Skip to content

Commit

Permalink
feature: NetworkTransform interpolation can be disabled (#3378)
Browse files Browse the repository at this point in the history
* Interpolation off bools for NetworkTransform.

Gives a snap-like effect to position, rotation and scaling.

* Update Assets/Mirror/Components/NetworkTransformBase.cs

* Update NetworkTransformBase.cs

---------

Co-authored-by: mischa <[email protected]>
  • Loading branch information
JesusLuvsYooh and miwarnec authored Feb 16, 2023
1 parent 569938c commit e0e2626
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
26 changes: 21 additions & 5 deletions Assets/Mirror/Components/NetworkTransformBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ public abstract class NetworkTransformBase : NetworkBehaviour
public readonly SortedList<double, TransformSnapshot> serverSnapshots = new SortedList<double, TransformSnapshot>();

// selective sync //////////////////////////////////////////////////////
[Header("Selective Sync & Interpolation\nDon't change these at Runtime")]
[Header("Selective Sync\nDon't change these at Runtime")]
public bool syncPosition = true; // do not change at runtime!
public bool syncRotation = true; // do not change at runtime!
public bool syncScale = false; // do not change at runtime! rare. off by default.

// interpolation is on by default, but can be disabled to jump to
// the destination immediately. some projects need this.
[Header("Interpolation")]
[Tooltip("Set to false to have a snap-like effect on position movement.")]
public bool interpolatePosition = true;
[Tooltip("Set to false to have a snap-like effect on rotations.")]
public bool interpolateRotation = true;
[Tooltip("Set to false to remove scale smoothing. Example use-case: Instant flipping of sprites that use -X and +X for direction.")]
public bool interpolateScale = true;

// debugging ///////////////////////////////////////////////////////////
[Header("Debug")]
public bool showGizmos;
Expand Down Expand Up @@ -141,7 +151,7 @@ protected void AddSnapshot(SortedList<double, TransformSnapshot> snapshots, doub
//
// NOTE: stuck detection is unnecessary here.
// we always set transform.position anyway, we can't get stuck.
protected virtual void Apply(TransformSnapshot interpolated)
protected virtual void Apply(TransformSnapshot interpolated, TransformSnapshot endGoal)
{
// local position/rotation for VR support
//
Expand All @@ -150,9 +160,15 @@ protected virtual void Apply(TransformSnapshot interpolated)
// -> we still interpolated
// -> but simply don't apply it. if the user doesn't want to sync
// scale, then we should not touch scale etc.
if (syncPosition) target.localPosition = interpolated.position;
if (syncRotation) target.localRotation = interpolated.rotation;
if (syncScale) target.localScale = interpolated.scale;

if (syncPosition)
target.localPosition = interpolatePosition ? interpolated.position : endGoal.position;

if (syncRotation)
target.localRotation = interpolateRotation ? interpolated.rotation : endGoal.rotation;

if (syncScale)
target.localScale = interpolateScale ? interpolated.scale : endGoal.scale;
}

// client->server teleport to force position without interpolation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void UpdateServer()

// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ void UpdateClient()

// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void UpdateServer()

// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ void UpdateClient()

// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Tests/Editor/NetworkTransform2kTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class NetworkTransformExposed : NetworkTransform
{
public new TransformSnapshot Construct() => base.Construct();
public new void Apply(TransformSnapshot interpolated) =>
base.Apply(interpolated);
base.Apply(interpolated, interpolated);
public new void OnClientToServerSync(Vector3? position, Quaternion? rotation, Vector3? scale) =>
base.OnClientToServerSync(position, rotation, scale);
public new void OnServerToClientSync(Vector3? position, Quaternion? rotation, Vector3? scale) =>
Expand Down

0 comments on commit e0e2626

Please sign in to comment.