mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
feat: Unity 6 Compatibility (#3820)
* fix(Rigidbody): Unity 6 Updates
Unity 6 Preview auto-applies the following changes:
- velocity -> linearVelocity
- drag -> linearDamping
- angularDrag -> angularDamping
* Added Rigidbody Extensions for backwards compatibility
* Revert "Added Rigidbody Extensions for backwards compatibility"
This reverts commit 4f699be7d4
.
* Backwards Compatibility
* Backwards Compatibility
* velocity -> linearVelocity
* Backwards compatibility
* fix(NetworkRigidbody2D) isKinematic -> bodyType
This commit is contained in:
parent
20992a8629
commit
8e905d70ab
@ -36,7 +36,11 @@ protected override void Awake()
|
||||
Debug.LogError($"{name}'s NetworkRigidbody2D.target {target.name} is missing a Rigidbody2D", this);
|
||||
return;
|
||||
}
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
wasKinematic = rb.bodyType.HasFlag(RigidbodyType2D.Kinematic);
|
||||
#else
|
||||
wasKinematic = rb.isKinematic;
|
||||
#endif
|
||||
base.Awake();
|
||||
}
|
||||
|
||||
@ -45,8 +49,13 @@ protected override void Awake()
|
||||
// for example, a game may run as client, set rigidbody.iskinematic=true,
|
||||
// then run as server, where .iskinematic isn't touched and remains at
|
||||
// the overwritten=true, even though the user set it to false originally.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
public override void OnStopServer() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
|
||||
public override void OnStopClient() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
|
||||
#else
|
||||
public override void OnStopServer() => rb.isKinematic = wasKinematic;
|
||||
public override void OnStopClient() => rb.isKinematic = wasKinematic;
|
||||
#endif
|
||||
|
||||
// overwriting Construct() and Apply() to set Rigidbody.MovePosition
|
||||
// would give more jittery movement.
|
||||
@ -69,7 +78,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
// client only
|
||||
else if (isClient)
|
||||
@ -81,7 +94,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
// server only
|
||||
else if (isServer)
|
||||
@ -92,7 +109,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,12 @@ protected override void Awake()
|
||||
Debug.LogError($"{name}'s NetworkRigidbody2D.target {target.name} is missing a Rigidbody2D", this);
|
||||
return;
|
||||
}
|
||||
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
wasKinematic = rb.bodyType.HasFlag(RigidbodyType2D.Kinematic);
|
||||
#else
|
||||
wasKinematic = rb.isKinematic;
|
||||
#endif
|
||||
base.Awake();
|
||||
}
|
||||
|
||||
@ -46,9 +51,13 @@ protected override void Awake()
|
||||
// for example, a game may run as client, set rigidbody.iskinematic=true,
|
||||
// then run as server, where .iskinematic isn't touched and remains at
|
||||
// the overwritten=true, even though the user set it to false originally.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
public override void OnStopServer() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
|
||||
public override void OnStopClient() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
|
||||
#else
|
||||
public override void OnStopServer() => rb.isKinematic = wasKinematic;
|
||||
public override void OnStopClient() => rb.isKinematic = wasKinematic;
|
||||
|
||||
#endif
|
||||
// overwriting Construct() and Apply() to set Rigidbody.MovePosition
|
||||
// would give more jittery movement.
|
||||
|
||||
@ -70,7 +79,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
// client only
|
||||
else if (isClient)
|
||||
@ -82,7 +95,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
// server only
|
||||
else if (isServer)
|
||||
@ -93,7 +110,11 @@ void FixedUpdate()
|
||||
// only set to kinematic if we don't own it
|
||||
// otherwise don't touch isKinematic.
|
||||
// the authority owner might use it either way.
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (!owned) rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
#else
|
||||
if (!owned) rb.isKinematic = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -423,9 +423,12 @@ protected virtual bool IsMoving() =>
|
||||
// predictedRigidbody.velocity.magnitude >= motionSmoothingVelocityThreshold ||
|
||||
// predictedRigidbody.angularVelocity.magnitude >= motionSmoothingAngularVelocityThreshold;
|
||||
// faster implementation with cached ²
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
predictedRigidbody.linearVelocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr ||
|
||||
#else
|
||||
predictedRigidbody.velocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr ||
|
||||
#endif
|
||||
predictedRigidbody.angularVelocity.sqrMagnitude >= motionSmoothingAngularVelocityThresholdSqr;
|
||||
|
||||
// TODO maybe merge the IsMoving() checks & callbacks with UpdateState().
|
||||
void UpdateGhosting()
|
||||
{
|
||||
@ -583,7 +586,11 @@ void RecordState()
|
||||
// grab current position/rotation/velocity only once.
|
||||
// this is performance critical, avoid calling .transform multiple times.
|
||||
tf.GetPositionAndRotation(out Vector3 currentPosition, out Quaternion currentRotation); // faster than accessing .position + .rotation manually
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
Vector3 currentVelocity = predictedRigidbody.linearVelocity;
|
||||
#else
|
||||
Vector3 currentVelocity = predictedRigidbody.velocity;
|
||||
#endif
|
||||
Vector3 currentAngularVelocity = predictedRigidbody.angularVelocity;
|
||||
|
||||
// calculate delta to previous state (if any)
|
||||
@ -638,8 +645,13 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3
|
||||
// hard snap to the position below a threshold velocity.
|
||||
// this is fine because the visual object still smoothly interpolates to it.
|
||||
// => consider both velocity and angular velocity (in case of Rigidbodies only rotating with joints etc.)
|
||||
if (predictedRigidbody.velocity.magnitude <= snapThreshold &&
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
if (predictedRigidbody.linearVelocity.magnitude <= snapThreshold &&
|
||||
predictedRigidbody.angularVelocity.magnitude <= snapThreshold)
|
||||
#else
|
||||
if (predictedRigidbody.velocity.magnitude <= snapThreshold &&
|
||||
predictedRigidbody.angularVelocity.magnitude <= snapThreshold)
|
||||
#endif
|
||||
{
|
||||
// Debug.Log($"Prediction: snapped {name} into place because velocity {predictedRigidbody.velocity.magnitude:F3} <= {snapThreshold:F3}");
|
||||
|
||||
@ -652,7 +664,11 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3
|
||||
// projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error
|
||||
if (!predictedRigidbody.isKinematic)
|
||||
{
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
predictedRigidbody.linearVelocity = velocity;
|
||||
#else
|
||||
predictedRigidbody.velocity = velocity;
|
||||
#endif
|
||||
predictedRigidbody.angularVelocity = angularVelocity;
|
||||
}
|
||||
|
||||
@ -704,7 +720,11 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3
|
||||
// (projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error)
|
||||
if (!predictedRigidbody.isKinematic)
|
||||
{
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
predictedRigidbody.linearVelocity = velocity;
|
||||
#else
|
||||
predictedRigidbody.velocity = velocity;
|
||||
#endif
|
||||
predictedRigidbody.angularVelocity = angularVelocity;
|
||||
}
|
||||
}
|
||||
@ -896,7 +916,11 @@ public override void OnSerialize(NetworkWriter writer, bool initialState)
|
||||
Time.deltaTime,
|
||||
position,
|
||||
rotation,
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
predictedRigidbody.linearVelocity,
|
||||
#else
|
||||
predictedRigidbody.velocity,
|
||||
#endif
|
||||
predictedRigidbody.angularVelocity);//,
|
||||
// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
|
||||
// predictedRigidbody.IsSleeping());
|
||||
|
@ -21,8 +21,13 @@ public static void MoveRigidbody(GameObject source, GameObject destination, bool
|
||||
|
||||
// copy all properties
|
||||
rigidbodyCopy.mass = original.mass;
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidbodyCopy.linearDamping = original.linearDamping;
|
||||
rigidbodyCopy.angularDamping = original.angularDamping;
|
||||
#else
|
||||
rigidbodyCopy.drag = original.drag;
|
||||
rigidbodyCopy.angularDrag = original.angularDrag;
|
||||
#endif
|
||||
rigidbodyCopy.useGravity = original.useGravity;
|
||||
rigidbodyCopy.isKinematic = original.isKinematic;
|
||||
rigidbodyCopy.interpolation = original.interpolation;
|
||||
@ -40,7 +45,11 @@ public static void MoveRigidbody(GameObject source, GameObject destination, bool
|
||||
// projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error
|
||||
if (!original.isKinematic)
|
||||
{
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidbodyCopy.linearVelocity = original.linearVelocity;
|
||||
#else
|
||||
rigidbodyCopy.velocity = original.velocity;
|
||||
#endif
|
||||
rigidbodyCopy.angularVelocity = original.angularVelocity;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,11 @@ void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Rigidbody rigidBody = predicted.predictedRigidbody;
|
||||
rigidBody.position = white.startPosition;
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidBody.linearVelocity = Vector3.zero;
|
||||
#else
|
||||
rigidBody.velocity = Vector3.zero;
|
||||
#endif
|
||||
}
|
||||
|
||||
// is it a read ball?
|
||||
|
@ -68,7 +68,11 @@ void Update()
|
||||
{
|
||||
if (Input.GetKey(KeyCode.Space) || Input.GetKeyDown(jumpKey))
|
||||
{
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpSpeed);
|
||||
#else
|
||||
rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,8 +86,11 @@ void Update()
|
||||
{
|
||||
movementVelocity = movementSpeed;
|
||||
}
|
||||
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rb.linearVelocity = new Vector2(movementVelocity, rb.linearVelocity.y);
|
||||
#else
|
||||
rb.velocity = new Vector2(movementVelocity, rb.velocity.y);
|
||||
#endif
|
||||
}
|
||||
|
||||
[ClientCallback]
|
||||
|
@ -15,7 +15,11 @@ public override void OnStartServer()
|
||||
rigidbody2d.simulated = true;
|
||||
|
||||
// Serve the ball from left player
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidbody2d.linearVelocity = Vector2.right * speed;
|
||||
#else
|
||||
rigidbody2d.velocity = Vector2.right * speed;
|
||||
#endif
|
||||
}
|
||||
|
||||
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
|
||||
@ -54,7 +58,11 @@ void OnCollisionEnter2D(Collision2D col)
|
||||
Vector2 dir = new Vector2(x, y).normalized;
|
||||
|
||||
// Set Velocity with dir * speed
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidbody2d.linearVelocity = dir * speed;
|
||||
#else
|
||||
rigidbody2d.velocity = dir * speed;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,11 @@ void FixedUpdate()
|
||||
// only let the local player control the racket.
|
||||
// don't control other player's rackets
|
||||
if (isLocalPlayer)
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
rigidbody2d.linearVelocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
|
||||
#else
|
||||
rigidbody2d.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user