mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
MovePosition/Rotation
This commit is contained in:
parent
0c1cab6ea2
commit
0b00351822
@ -13,11 +13,32 @@ public class PredictedRigidbody : NetworkBehaviour
|
|||||||
[Tooltip("Broadcast changes if position changed by more than ... meters.")]
|
[Tooltip("Broadcast changes if position changed by more than ... meters.")]
|
||||||
public float positionSensitivity = 0.01f;
|
public float positionSensitivity = 0.01f;
|
||||||
|
|
||||||
|
[Header("Smoothing")]
|
||||||
|
public bool smoothCorrection = true;
|
||||||
|
|
||||||
void Awake()
|
void Awake()
|
||||||
{
|
{
|
||||||
rb = GetComponent<Rigidbody>();
|
rb = GetComponent<Rigidbody>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity)
|
||||||
|
{
|
||||||
|
// Rigidbody .position teleports, while .MovePosition interpolates
|
||||||
|
// TODO is this a good idea? what about next capture while it's interpolating?
|
||||||
|
if (smoothCorrection)
|
||||||
|
{
|
||||||
|
rb.MovePosition(position);
|
||||||
|
rb.MoveRotation(rotation);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rb.position = position;
|
||||||
|
rb.rotation = rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
rb.velocity = velocity;
|
||||||
|
}
|
||||||
|
|
||||||
void UpdateServer()
|
void UpdateServer()
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity)
|
if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity)
|
||||||
@ -51,10 +72,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
|
|||||||
Vector3 velocity = reader.ReadVector3();
|
Vector3 velocity = reader.ReadVector3();
|
||||||
|
|
||||||
// hard force for now.
|
// hard force for now.
|
||||||
// TODO compare past position at timestamp, and only correct if needed
|
ApplyState(position, rotation, velocity);
|
||||||
rb.position = position;
|
|
||||||
rb.rotation = rotation;
|
|
||||||
rb.velocity = velocity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user