use new approach to sync vehicles

This commit is contained in:
in0finite 2022-05-04 03:44:19 +02:00
parent d49ef04a30
commit d0bd858cce
6 changed files with 67 additions and 124 deletions

View file

@ -10,12 +10,13 @@ GameObject:
m_Component:
- component: {fileID: 8267877565696799342}
- component: {fileID: 6732552431618800415}
- component: {fileID: 459295042993608392}
- component: {fileID: 9183683351129218624}
- component: {fileID: 6154053831592577151}
- component: {fileID: 575085982327711637}
- component: {fileID: 6891581410426086992}
- component: {fileID: 1742792654273641741}
- component: {fileID: 9038525027628313851}
- component: {fileID: 6804370665295485595}
m_Layer: 0
m_Name: Vehicle
m_TagString: Untagged
@ -54,40 +55,6 @@ MonoBehaviour:
visible: 0
m_AssetId: abddce9c98c1ba5a992f109cfd8c5a5c
hasSpawned: 0
--- !u!114 &459295042993608392
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5841438317718401360}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2f74aedd71d9a4f55b3ce499326d45fb, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.05
clientAuthority: 0
sendInterval: 0.05
syncPosition: 1
syncRotation: 1
syncScale: 0
interpolatePosition: 1
interpolateRotation: 1
interpolateScale: 0
bufferTimeMultiplier: 1
bufferSizeLimit: 64
catchupThreshold: 4
catchupMultiplier: 0.1
onlySyncOnChange: 1
bufferResetMultiplier: 5
positionSensitivity: 0.01
rotationSensitivity: 0.01
scaleSensitivity: 0.01
showGizmos: 0
showOverlay: 0
overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
--- !u!114 &9183683351129218624
MonoBehaviour:
m_ObjectHideFlags: 0
@ -102,6 +69,31 @@ MonoBehaviour:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.05
--- !u!114 &6154053831592577151
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5841438317718401360}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7810004c3617c05419e4e60874dc18a7, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.05
m_transformToSync: {fileID: 8267877565696799342}
m_transformSyncParameters:
useSmoothDeltaTime: 1
clientUpdateType: 3
constantVelocityMultiplier: 1
lerpFactor: 30
useRigidBody: 0
visualize: 0
maxNumVisualizations: 10
visualizationScale: 0.2
snapshotLatency: 0.1
--- !u!114 &575085982327711637
MonoBehaviour:
m_ObjectHideFlags: 0
@ -240,3 +232,19 @@ MonoBehaviour:
m_EditorClassIdentifier:
componentsToDestroy:
- {fileID: 1742792654273641741}
--- !u!54 &6804370665295485595
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5841438317718401360}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0

View file

@ -153,7 +153,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public Mirror.NetworkIdentity NetIdentity { get; private set; }
public Mirror.NetworkTransform NetTransform { get; private set; }
public Net.CustomNetworkTransform NetTransform { get; private set; }
List<Ped> m_lastPreparedPeds = new List<Ped>();
@ -173,7 +173,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
private void Awake()
{
this.NetIdentity = this.GetComponentOrThrow<Mirror.NetworkIdentity>();
this.NetTransform = this.GetComponent<Mirror.NetworkTransform>();
this.NetTransform = this.GetComponent<Net.CustomNetworkTransform>();
_props = new MaterialPropertyBlock();
this.Awake_Damage();
this.Awake_Radio();

View file

@ -21,14 +21,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles
[SyncVar] float m_net_steering;
[SyncVar] float m_net_braking;
[SyncVar(hook = nameof(OnNetPositionChanged))]
Vector3 m_net_position;
[SyncVar(hook = nameof(OnNetRotationChanged))]
Quaternion m_net_rotation;
[SyncVar] Vector3 m_net_linearVelocity;
[SyncVar] Vector3 m_net_angularVelocity;
[SyncVar] float m_net_health;
struct WheelSyncData
@ -104,6 +96,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
Destroy(wheelCollider);
}
}
if (VehicleManager.Instance.destroyRigidBodyOnClients)
{
if (m_vehicle.RigidBody != null)
Object.Destroy(m_vehicle.RigidBody);
}
});
}
}
@ -165,9 +163,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles
private void Update()
{
// if syncvars are used for updating transform, then disable NetworkTransform, and vice versa
m_vehicle.NetTransform.enabled = !VehicleManager.Instance.syncVehicleTransformUsingSyncVars;
// update status of rigid body
this.EnableOrDisableRigidBody();
@ -221,10 +216,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles
m_net_acceleration = m_vehicle.Accelerator;
m_net_steering = m_vehicle.Steering;
m_net_braking = m_vehicle.Braking;
m_net_position = m_vehicle.RigidBody.position;
m_net_rotation = m_vehicle.RigidBody.rotation;
m_net_linearVelocity = m_vehicle.RigidBody.velocity;
m_net_angularVelocity = m_vehicle.RigidBody.angularVelocity;
m_net_health = m_vehicle.Health;
// wheels
@ -277,21 +268,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles
}
}
// position and rotation will be applied in syncvar hooks
// apply velocity on all clients
if (VehicleManager.Instance.syncLinearVelocity)
m_vehicle.RigidBody.velocity = m_net_linearVelocity;
else
m_vehicle.RigidBody.velocity = Vector3.zero;
if (VehicleManager.Instance.syncAngularVelocity)
m_vehicle.RigidBody.angularVelocity = m_net_angularVelocity;
else
m_vehicle.RigidBody.angularVelocity = Vector3.zero;
m_vehicle.Health = m_net_health;
}
}
@ -327,37 +303,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles
if (NetStatus.IsServer)
{
F.EnableRigidBody(m_vehicle.RigidBody);
return;
}
if (VehicleManager.Instance.whenToDisableRigidBody.Matches(this.IsControlledByLocalPlayer,
!NetStatus.IsServer))
F.DisableRigidBody(m_vehicle.RigidBody);
else
F.EnableRigidBody(m_vehicle.RigidBody);
}
void OnNetPositionChanged(Vector3 oldPos, Vector3 newPos)
{
if (NetStatus.IsServer)
return;
if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars)
{
if (m_vehicle != null && m_vehicle.RigidBody != null)
m_vehicle.RigidBody.MovePosition(newPos);
}
}
void OnNetRotationChanged(Quaternion oldRot, Quaternion newRot)
{
if (NetStatus.IsServer)
return;
if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars)
{
if (m_vehicle != null && m_vehicle.RigidBody != null)
m_vehicle.RigidBody.MoveRotation(newRot);
}
}

View file

@ -18,9 +18,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public bool syncLinearVelocity = true;
public bool syncAngularVelocity = true;
public Utilities.WhenOnClient whenToDisableRigidBody = Utilities.WhenOnClient.OnlyOnOtherClients;
public bool destroyRigidBodyOnClients = true;
public bool syncPedTransformWhileInVehicle = false;
public bool syncVehicleTransformUsingSyncVars = false;
public bool controlInputOnLocalPlayer = true;
public bool controlWheelsOnLocalPlayer = true;
public bool destroyWheelCollidersOnClient = true;

View file

@ -6,7 +6,6 @@ using VConsts = SanAndreasUnity.Behaviours.Vehicles.VehiclePhysicsConstants;
namespace SanAndreasUnity.Behaviours.Vehicles
{
[RequireComponent(typeof(Rigidbody))]
public partial class Vehicle
{
private Rigidbody _rigidBody;
@ -21,7 +20,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
[Range(0, 1)]
public float Braking = 1f;
public Vector3 Velocity { get { return _rigidBody.velocity; } }
public Vector3 Velocity { get { return _rigidBody != null ? _rigidBody.velocity : Vector3.zero; } }
public float AverageWheelHeight { get { return _wheels.Count == 0 ? transform.position.y : _wheels.Average(x => x.Child.position.y); } }
@ -73,12 +72,18 @@ namespace SanAndreasUnity.Behaviours.Vehicles
private void UpdateValues(VConsts vals)
{
_rigidBody.drag = HandlingData.Drag * vals.DragScale;
_rigidBody.mass = HandlingData.Mass * vals.MassScale;
_rigidBody.centerOfMass = HandlingData.CentreOfMass;
if (_rigidBody != null)
{
_rigidBody.drag = HandlingData.Drag * vals.DragScale;
_rigidBody.mass = HandlingData.Mass * vals.MassScale;
_rigidBody.centerOfMass = HandlingData.CentreOfMass;
}
foreach (var wheel in _wheels)
{
if (null == wheel.Collider)
continue;
var spring = wheel.Collider.suspensionSpring;
spring.damper = HandlingData.SuspensionDampingLevel * vals.SuspensionDampingScale;

View file

@ -39,12 +39,6 @@ namespace SanAndreasUnity.Settings
setValue = (value) => { ApplyVehicleSyncRate(value); },
persistType = OptionsWindow.InputPersistType.OnStart
};
OptionsWindow.BoolInput m_syncVehicleTransformUsingSyncVars = new OptionsWindow.BoolInput ("Sync vehicle transform using syncvars") {
isAvailable = () => VehicleManager.Instance != null,
getValue = () => VehicleManager.Instance.syncVehicleTransformUsingSyncVars,
setValue = (value) => { VehicleManager.Instance.syncVehicleTransformUsingSyncVars = value; },
persistType = OptionsWindow.InputPersistType.OnStart
};
OptionsWindow.BoolInput m_syncVehiclesLinearVelocity = new OptionsWindow.BoolInput ("Sync vehicle's linear velocity") {
isAvailable = () => VehicleManager.Instance != null,
getValue = () => VehicleManager.Instance.syncLinearVelocity,
@ -76,11 +70,11 @@ namespace SanAndreasUnity.Settings
setValue = (value) => { VehicleManager.Instance.controlInputOnLocalPlayer = value; },
persistType = OptionsWindow.InputPersistType.OnStart
};
OptionsWindow.EnumInput<WhenOnClient> m_whenToDisableVehiclesRigidBody = new OptionsWindow.EnumInput<WhenOnClient> () {
description = "When to disable vehicle rigid body on clients",
OptionsWindow.BoolInput m_destroyVehiclesRigidBodyOnClients = new OptionsWindow.BoolInput () {
description = "Destroy vehicle rigid body on clients",
isAvailable = () => VehicleManager.Instance != null,
getValue = () => VehicleManager.Instance.whenToDisableRigidBody,
setValue = (value) => { ApplyRigidBodyState(value); },
getValue = () => VehicleManager.Instance.destroyRigidBodyOnClients,
setValue = (value) => VehicleManager.Instance.destroyRigidBodyOnClients = value,
persistType = OptionsWindow.InputPersistType.OnStart
};
OptionsWindow.BoolInput m_syncPedTransformWhileInVehicle = new OptionsWindow.BoolInput ("Sync ped transform while in vehicle") {
@ -111,13 +105,12 @@ namespace SanAndreasUnity.Settings
m_deadBodySyncRate,
m_deadBodyInterpolationMode,
m_vehicleSyncRate,
m_syncVehicleTransformUsingSyncVars,
m_syncVehiclesLinearVelocity,
m_syncVehiclesAngularVelocity,
m_controlWheelsOnLocalPlayer,
m_destroyWheelCollidersOnClient,
m_controlVehicleInputOnLocalPlayer,
m_whenToDisableVehiclesRigidBody,
m_destroyVehiclesRigidBodyOnClients,
m_vehicleRigidBodyInterpolationModeOnServer,
m_vehicleRigidBodyInterpolationModeOnClient,
m_syncPedTransformWhileInVehicle);
@ -159,13 +152,6 @@ namespace SanAndreasUnity.Settings
}
}
static void ApplyRigidBodyState(WhenOnClient when)
{
VehicleManager.Instance.whenToDisableRigidBody = when;
foreach (var v in Vehicle.AllVehicles)
v.GetComponent<VehicleController>().EnableOrDisableRigidBody();
}
static void ApplyRigidBodyInterpolationModeOnServer(RigidbodyInterpolation interpolation)
{
VehicleManager.Instance.rigidbodyInterpolationOnServer = interpolation;