Sync rigid body's linear and angular velocity

This commit is contained in:
in0finite 2019-04-30 02:37:29 +02:00
parent 9a01c2464a
commit a7e0c91f31
3 changed files with 12 additions and 0 deletions

View file

@ -14,6 +14,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
[SyncVar] float m_net_acceleration;
[SyncVar] float m_net_steering;
[SyncVar] float m_net_braking;
[SyncVar] Vector3 m_net_linearVelocity;
[SyncVar] Vector3 m_net_angularVelocity;
// is it better to place syncvars in Vehicle class ? - that way, there is no need for hooks
// - or we could assign/read syncvars in Update()
@ -79,12 +81,18 @@ namespace SanAndreasUnity.Behaviours.Vehicles
m_net_acceleration = m_vehicle.Accelerator;
m_net_steering = m_vehicle.Steering;
m_net_braking = m_vehicle.Braking;
m_net_linearVelocity = m_vehicle.RigidBody.velocity;
m_net_angularVelocity = m_vehicle.RigidBody.angularVelocity;
}
else
{
m_vehicle.Accelerator = m_net_acceleration;
m_vehicle.Steering = m_net_steering;
m_vehicle.Braking = m_net_braking;
if (VehicleManager.Instance.syncLinearVelocity)
m_vehicle.RigidBody.velocity = m_net_linearVelocity;
if (VehicleManager.Instance.syncAngularVelocity)
m_vehicle.RigidBody.angularVelocity = m_net_angularVelocity;
}
}

View file

@ -8,6 +8,9 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public GameObject vehiclePrefab;
public bool syncLinearVelocity = true;
public bool syncAngularVelocity = true;
void Awake()
{

View file

@ -10,6 +10,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public partial class Vehicle
{
private Rigidbody _rigidBody;
public Rigidbody RigidBody => _rigidBody;
[Range(-1, 1)]
public float Accelerator;