This commit is contained in:
MrGadget 2024-11-04 23:54:58 +01:00 committed by GitHub
commit bc43fe9272
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 2 deletions

View File

@ -156,6 +156,54 @@ protected virtual void Awake()
Configure();
}
// These are in global coordinates for calculating velocity and angular velocity.
Vector3 lastPosition;
Quaternion lastRotation;
public Vector3 velocity { get; internal set; } = Vector3.zero;
public Vector3 angularVelocity { get; internal set; } = Vector3.zero;
protected virtual void Start()
{
// set target to self if none yet
if (target == null) target = transform;
// Set last position and rotation to current values
// so we can calculate velocity and angular velocity.
target.GetPositionAndRotation(out lastPosition, out lastRotation);
}
protected virtual void Update()
{
// set target to self if none yet
if (target == null) target = transform;
// Use global coordinates for velocity and angular velocity.
target.GetPositionAndRotation(out Vector3 pos, out Quaternion rot);
// Update velocity and angular velocity
velocity = (pos - lastPosition) / Time.deltaTime;
CalculateAngularVelocity(rot);
// Update last position and rotation
lastPosition = pos;
lastRotation = rot;
}
void CalculateAngularVelocity(Quaternion currentRot)
{
// calculate angle between two rotations
Quaternion deltaRotation = currentRot * Quaternion.Inverse(lastRotation);
//Quaternion deltaRotation = (currentRot * Quaternion.Inverse(lastRotation)).normalize;
// convert to angle axis
deltaRotation.ToAngleAxis(out float angle, out Vector3 axis);
// we assume the angle is always the shortest path
// we don't need to check for 360 degree rotations
angularVelocity = axis * angle * Mathf.Deg2Rad / Time.deltaTime;
}
// snapshot functions //////////////////////////////////////////////////
// get local/world position
protected virtual Vector3 GetPosition() =>
@ -414,6 +462,16 @@ public virtual void ResetState()
// so let's clear the buffers.
serverSnapshots.Clear();
clientSnapshots.Clear();
// set target to self if none yet
if (target == null) target = transform;
// Reset last position and rotation
target.GetPositionAndRotation(out lastPosition, out lastRotation);
// Reset velocity / angular velocity
velocity = Vector3.zero;
angularVelocity = Vector3.zero;
}
public virtual void Reset()

View File

@ -43,13 +43,15 @@ public class NetworkTransformReliable : NetworkTransformBase
protected TransformSnapshot last;
// update //////////////////////////////////////////////////////////////
void Update()
protected override void Update()
{
// if server then always sync to others.
if (isServer) UpdateServer();
// 'else if' because host mode shouldn't send anything to server.
// it is the server. don't overwrite anything there.
else if (isClient) UpdateClient();
base.Update();
}
void LateUpdate()

View File

@ -27,13 +27,15 @@ public class NetworkTransformUnreliable : NetworkTransformBase
// update //////////////////////////////////////////////////////////////
// Update applies interpolation
void Update()
protected override void Update()
{
if (isServer) UpdateServerInterpolation();
// for all other clients (and for local player if !authority),
// we need to apply snapshots from the buffer.
// 'else if' because host mode shouldn't interpolate client
else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation();
base.Update();
}
// LateUpdate broadcasts.