SanAndreasUnity/Assets/Scripts/Networking/NetworkRigidBody.cs

157 lines
4.2 KiB
C#
Raw Normal View History

2020-07-01 21:32:40 +00:00
using Mirror;
using UnityEngine;
namespace SanAndreasUnity.Net
{
public class NetworkRigidBody : NetworkBehaviour
{
private Rigidbody _rigidbody;
public Rigidbody Rigidbody
{
get => _rigidbody;
set
{
if (_rigidbody == value)
return;
_rigidbody = value;
if (NetStatus.IsClientOnly)
{
this.UpdateAllPropertiesOnClient();
}
}
}
2020-07-01 21:32:40 +00:00
2021-02-05 22:21:39 +00:00
[SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position = Vector3.zero;
[SyncVar(hook=nameof(OnNetRotationChanged))] Vector3 m_net_rotation = Vector3.zero;
2020-07-01 22:59:17 +00:00
[SyncVar] Vector3 m_net_velocity = Vector3.zero;
[SyncVar] Vector3 m_net_angularVelocity = Vector3.zero;
2021-02-05 22:21:39 +00:00
public bool disableCollisionDetectionOnClients = true;
public bool disableGravityOnClients = true;
2020-07-01 21:32:40 +00:00
void Awake()
{
this.Rigidbody = this.GetComponent<Rigidbody>();
2020-07-01 22:59:17 +00:00
if (NetStatus.IsServer)
{
2021-02-05 22:21:39 +00:00
// assign syncvars before the object is spawned on network
2020-07-01 22:59:17 +00:00
this.UpdateServer();
}
2021-02-05 22:21:39 +00:00
// suggest that interpolation should be changed
if (!NetStatus.IsServer)
2020-07-01 22:59:17 +00:00
{
2021-02-05 22:21:39 +00:00
if (this.Rigidbody != null && this.Rigidbody.interpolation == RigidbodyInterpolation.None)
Debug.LogWarning($"For better sync, interpolation should be changed, rigid body: {this.Rigidbody.name}");
2020-07-01 22:59:17 +00:00
}
2020-07-01 21:32:40 +00:00
}
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();
}
2021-02-05 22:21:39 +00:00
void Update()
{
if (NetStatus.IsServer)
this.UpdateServer();
else
this.UpdateClient();
}
2020-07-01 22:59:17 +00:00
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;
}
}
private void UpdateClient()
2020-07-01 21:32:40 +00:00
{
if (null == this.Rigidbody)
return;
2021-02-05 22:21:39 +00:00
if (this.disableCollisionDetectionOnClients)
this.Rigidbody.detectCollisions = false;
if (this.disableGravityOnClients)
this.Rigidbody.useGravity = false;
// position and rotation are updated in syncvar hooks
2020-07-01 22:59:17 +00:00
this.Rigidbody.velocity = m_net_velocity;
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();
}
2021-02-05 22:21:39 +00:00
void OnNetPositionChanged(Vector3 pos)
{
if (NetStatus.IsServer)
return;
if (null == this.Rigidbody)
return;
this.Rigidbody.MovePosition(pos);
}
void OnNetRotationChanged(Vector3 eulers)
{
if (NetStatus.IsServer)
return;
if (null == this.Rigidbody)
return;
this.Rigidbody.MoveRotation(Quaternion.Euler(eulers));
}
2020-07-01 21:32:40 +00:00
}
}