fix for wrong position of detached vehicle parts

This commit is contained in:
in0finite 2021-02-14 15:13:39 +01:00
parent eac9bce0c7
commit 38e23851c6
2 changed files with 43 additions and 6 deletions

View file

@ -6,7 +6,23 @@ namespace SanAndreasUnity.Net
public class NetworkRigidBody : NetworkBehaviour
{
public Rigidbody Rigidbody { get; set; }
private Rigidbody _rigidbody;
public Rigidbody Rigidbody
{
get => _rigidbody;
set
{
if (_rigidbody == value)
return;
_rigidbody = value;
if (NetStatus.IsClientOnly)
{
this.UpdateAllPropertiesOnClient();
}
}
}
[SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position = Vector3.zero;
[SyncVar(hook=nameof(OnNetRotationChanged))] Vector3 m_net_rotation = Vector3.zero;
@ -35,6 +51,17 @@ namespace SanAndreasUnity.Net
}
}
public override void OnStartClient()
{
if (NetStatus.IsServer)
return;
// need to apply initial syncvar values, because otherwise the object may stay on the place where it
// was originally spawned on the server (if object doesn't change position, syncvars will not be updated)
this.UpdateAllPropertiesOnClient();
}
void Update()
{
if (NetStatus.IsServer)
@ -75,7 +102,7 @@ namespace SanAndreasUnity.Net
}
public void UpdateClient()
private void UpdateClient()
{
if (null == this.Rigidbody)
return;
@ -92,6 +119,16 @@ namespace SanAndreasUnity.Net
this.Rigidbody.angularVelocity = m_net_angularVelocity;
}
private void UpdateAllPropertiesOnClient()
{
if (null == this.Rigidbody)
return;
this.Rigidbody.MovePosition(m_net_position);
this.Rigidbody.MoveRotation(Quaternion.Euler(m_net_rotation));
this.UpdateClient();
}
void OnNetPositionChanged(Vector3 pos)
{
if (NetStatus.IsServer)

View file

@ -129,10 +129,10 @@ namespace SanAndreasUnity.Net
vehicle.DetachFrameDuringExplosion(m_net_frameName, m_net_mass, this.gameObject);
}
this.NetworkRigidBody.Rigidbody = this.GetComponentInChildren<Rigidbody>();
if (this.NetworkRigidBody.Rigidbody != null)
this.NetworkRigidBody.Rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
this.NetworkRigidBody.UpdateClient();
var rb = this.GetComponentInChildren<Rigidbody>();
if (rb != null)
rb.interpolation = RigidbodyInterpolation.Interpolate;
this.NetworkRigidBody.Rigidbody = rb;
}