mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-23 04:23:04 +00:00
rigid body sync works
This commit is contained in:
parent
798dfd55ed
commit
f82315f3ad
2 changed files with 79 additions and 5 deletions
|
@ -13,37 +13,108 @@ namespace SanAndreasUnity.Net
|
|||
Vector3 m_lastVelocity = Vector3.zero;
|
||||
Vector3 m_lastAngularVelocity = Vector3.zero;
|
||||
|
||||
[SyncVar] Vector3 m_net_position = Vector3.zero;
|
||||
[SyncVar] Vector3 m_net_rotation = Vector3.zero;
|
||||
[SyncVar] Vector3 m_net_velocity = Vector3.zero;
|
||||
[SyncVar] Vector3 m_net_angularVelocity = Vector3.zero;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
this.Rigidbody = this.GetComponent<Rigidbody>();
|
||||
|
||||
if (NetStatus.IsServer)
|
||||
{
|
||||
this.UpdateServer();
|
||||
this.InvokeRepeating(nameof(this.UpdateServer), 0.001f, this.syncInterval);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.InvokeRepeating(nameof(this.UpdateClient), 0.001f, this.syncInterval);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDeserialize(NetworkReader reader, bool initialState)
|
||||
public void UpdateServer()
|
||||
{
|
||||
if (null == this.Rigidbody)
|
||||
return;
|
||||
|
||||
Vector3 pos = this.Rigidbody.position;
|
||||
Vector3 rot = this.Rigidbody.rotation.eulerAngles;
|
||||
Vector3 vel = this.Rigidbody.velocity;
|
||||
Vector3 angVel = this.Rigidbody.angularVelocity;
|
||||
|
||||
if (pos != m_net_position)
|
||||
{
|
||||
m_net_position = pos;
|
||||
}
|
||||
|
||||
if (rot != m_net_rotation)
|
||||
{
|
||||
m_net_rotation = rot;
|
||||
}
|
||||
|
||||
if (vel != m_net_velocity)
|
||||
{
|
||||
m_net_velocity = vel;
|
||||
}
|
||||
|
||||
if (angVel != m_net_angularVelocity)
|
||||
{
|
||||
m_net_angularVelocity = angVel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void UpdateClient()
|
||||
{
|
||||
if (null == this.Rigidbody)
|
||||
return;
|
||||
|
||||
this.Rigidbody.MovePosition(m_net_position);
|
||||
this.Rigidbody.MoveRotation(Quaternion.Euler(m_net_rotation));
|
||||
this.Rigidbody.velocity = m_net_velocity;
|
||||
this.Rigidbody.angularVelocity = m_net_angularVelocity;
|
||||
}
|
||||
|
||||
/*
|
||||
public override void OnDeserialize(NetworkReader reader, bool initialState)
|
||||
{
|
||||
bool hasRigidBody = this.Rigidbody != null;
|
||||
|
||||
byte bitfield = reader.ReadByte();
|
||||
|
||||
//Vector3 pos = Vector3.zero;
|
||||
//Vector3 rot = Vector3.zero;
|
||||
//Vector3 vel = Vector3.zero;
|
||||
//Vector3 angVel = Vector3.zero;
|
||||
|
||||
if ((bitfield & 1) != 0)
|
||||
{
|
||||
this.Rigidbody.MovePosition(reader.ReadVector3());
|
||||
Vector3 pos = reader.ReadVector3();
|
||||
if (hasRigidBody)
|
||||
this.Rigidbody.MovePosition(pos);
|
||||
}
|
||||
|
||||
if ((bitfield & 2) != 0)
|
||||
{
|
||||
this.Rigidbody.MoveRotation(Quaternion.Euler(reader.ReadVector3()));
|
||||
Vector3 rot = reader.ReadVector3();
|
||||
if (hasRigidBody)
|
||||
this.Rigidbody.MoveRotation(Quaternion.Euler(rot));
|
||||
}
|
||||
|
||||
if ((bitfield & 4) != 0)
|
||||
{
|
||||
this.Rigidbody.velocity = reader.ReadVector3();
|
||||
Vector3 vel = reader.ReadVector3();
|
||||
if (hasRigidBody)
|
||||
this.Rigidbody.velocity = vel;
|
||||
}
|
||||
|
||||
if ((bitfield & 8) != 0)
|
||||
{
|
||||
this.Rigidbody.angularVelocity = reader.ReadVector3();
|
||||
Vector3 angVel = reader.ReadVector3();
|
||||
if (hasRigidBody)
|
||||
this.Rigidbody.angularVelocity = angVel;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -116,6 +187,7 @@ namespace SanAndreasUnity.Net
|
|||
|
||||
return bitfield != 0; // is dirty
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ namespace SanAndreasUnity.Net
|
|||
m_net_mass = mass;
|
||||
|
||||
this.NetworkRigidBody.Rigidbody = rigidbody;
|
||||
this.NetworkRigidBody.UpdateServer();
|
||||
}
|
||||
|
||||
public override void OnStartClient()
|
||||
|
@ -50,6 +51,7 @@ namespace SanAndreasUnity.Net
|
|||
Vehicle vehicle = vehicleGo.GetComponentOrThrow<Vehicle>();
|
||||
vehicle.DetachFrameDuringExplosion(m_net_frameName, m_net_mass, this.gameObject);
|
||||
this.NetworkRigidBody.Rigidbody = this.GetComponentInChildren<Rigidbody>();
|
||||
this.NetworkRigidBody.UpdateClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue