From 925934ccda0411f6a03721749317a0720180e817 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Thu, 29 Feb 2024 15:41:53 +0100 Subject: [PATCH] Display rope on client (We need to implement client prediction and reconciliation for rope) --- .../Networked/NetworkPrefabsList.asset | 10 ++ Assets/Prefabs/Networked/RopePoint.prefab | 152 ++++++++++++++++++ .../Prefabs/Networked/RopePoint.prefab.meta | 7 + Assets/Prefabs/Rope.prefab | 58 ++++++- Assets/Scenes/GameScene.unity | 22 +-- .../Multi-SetupLoad.unity | 23 +-- Assets/Scripts/Rope/Rope.cs | 1 + Assets/Scripts/Rope/RopeSimulator.cs | 50 +++--- 8 files changed, 275 insertions(+), 48 deletions(-) create mode 100644 Assets/Prefabs/Networked/RopePoint.prefab create mode 100644 Assets/Prefabs/Networked/RopePoint.prefab.meta diff --git a/Assets/Prefabs/Networked/NetworkPrefabsList.asset b/Assets/Prefabs/Networked/NetworkPrefabsList.asset index e74bc03..ae4a4e9 100644 --- a/Assets/Prefabs/Networked/NetworkPrefabsList.asset +++ b/Assets/Prefabs/Networked/NetworkPrefabsList.asset @@ -19,3 +19,13 @@ MonoBehaviour: SourcePrefabToOverride: {fileID: 0} SourceHashToOverride: 0 OverridingTargetPrefab: {fileID: 0} + - Override: 0 + Prefab: {fileID: 4736158799711156794, guid: 95a46812725b6db46a5dc3e853110b5e, type: 3} + SourcePrefabToOverride: {fileID: 0} + SourceHashToOverride: 0 + OverridingTargetPrefab: {fileID: 0} + - Override: 0 + Prefab: {fileID: 5991265243222894942, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} + SourcePrefabToOverride: {fileID: 0} + SourceHashToOverride: 0 + OverridingTargetPrefab: {fileID: 0} diff --git a/Assets/Prefabs/Networked/RopePoint.prefab b/Assets/Prefabs/Networked/RopePoint.prefab new file mode 100644 index 0000000..8fae474 --- /dev/null +++ b/Assets/Prefabs/Networked/RopePoint.prefab @@ -0,0 +1,152 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4736158799711156794 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 3817028170077760731} + - component: {fileID: 4590407122831754579} + - component: {fileID: 8963967235500975272} + - component: {fileID: -5174249022758144916} + - component: {fileID: -7862229988660046446} + m_Layer: 0 + m_Name: RopePoint + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &3817028170077760731 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4736158799711156794} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!58 &4590407122831754579 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4736158799711156794} + m_Enabled: 1 + serializedVersion: 3 + m_Density: 1 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_ForceSendLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ForceReceiveLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ContactCaptureLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_CallbackLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_CompositeOperation: 0 + m_CompositeOrder: 0 + m_Offset: {x: 0, y: 0} + m_Radius: 0.5 +--- !u!50 &8963967235500975272 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4736158799711156794} + m_BodyType: 1 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 1 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 +--- !u!114 &-5174249022758144916 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4736158799711156794} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 1634323294 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 +--- !u!114 &-7862229988660046446 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4736158799711156794} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} + m_Name: + m_EditorClassIdentifier: + UseUnreliableDeltas: 0 + SyncPositionX: 1 + SyncPositionY: 1 + SyncPositionZ: 1 + SyncRotAngleX: 0 + SyncRotAngleY: 0 + SyncRotAngleZ: 0 + SyncScaleX: 0 + SyncScaleY: 0 + SyncScaleZ: 0 + PositionThreshold: 0.001 + RotAngleThreshold: 0.01 + ScaleThreshold: 0.01 + UseQuaternionSynchronization: 0 + UseQuaternionCompression: 0 + UseHalfFloatPrecision: 0 + InLocalSpace: 0 + Interpolate: 1 + SlerpPosition: 0 diff --git a/Assets/Prefabs/Networked/RopePoint.prefab.meta b/Assets/Prefabs/Networked/RopePoint.prefab.meta new file mode 100644 index 0000000..4dcb744 --- /dev/null +++ b/Assets/Prefabs/Networked/RopePoint.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 95a46812725b6db46a5dc3e853110b5e +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Rope.prefab b/Assets/Prefabs/Rope.prefab index 2ad8370..c2d39d3 100644 --- a/Assets/Prefabs/Rope.prefab +++ b/Assets/Prefabs/Rope.prefab @@ -9,10 +9,12 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 144529238244638330} + - component: {fileID: 2894273998966960381} - component: {fileID: 7717684785049474632} - component: {fileID: 901761791259710742} - component: {fileID: 4976294692568481572} - component: {fileID: 2066655509941542230} + - component: {fileID: 7670104141912458081} m_Layer: 0 m_Name: Rope m_TagString: Untagged @@ -35,6 +37,27 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2894273998966960381 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5991265243222894942} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 3972580189 + InScenePlacedSourceGlobalObjectIdHash: 1619090677 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 0 --- !u!114 &7717684785049474632 MonoBehaviour: m_ObjectHideFlags: 0 @@ -91,10 +114,8 @@ MonoBehaviour: m_RotationOrder: 4 ropeMaxLength: 50 ropeMinLength: 20 + colliderPrefab: {fileID: 4736158799711156794, guid: 95a46812725b6db46a5dc3e853110b5e, type: 3} colliderTag: Rope - colliderLayer: - serializedVersion: 2 - m_Bits: 64 ropeCollidersParent: {fileID: 144529238244638330} lineRenderer: {fileID: 901761791259710742} pullAnimationOvershootThreshold: 0.2 @@ -312,3 +333,34 @@ AudioSource: m_PreInfinity: 2 m_PostInfinity: 2 m_RotationOrder: 4 +--- !u!114 &7670104141912458081 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5991265243222894942} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} + m_Name: + m_EditorClassIdentifier: + UseUnreliableDeltas: 0 + SyncPositionX: 1 + SyncPositionY: 1 + SyncPositionZ: 1 + SyncRotAngleX: 0 + SyncRotAngleY: 0 + SyncRotAngleZ: 0 + SyncScaleX: 0 + SyncScaleY: 0 + SyncScaleZ: 0 + PositionThreshold: 0.001 + RotAngleThreshold: 0.01 + ScaleThreshold: 0.01 + UseQuaternionSynchronization: 0 + UseQuaternionCompression: 0 + UseHalfFloatPrecision: 0 + InLocalSpace: 0 + Interpolate: 1 + SlerpPosition: 0 diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index fd292a8..5860260 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -3669,10 +3669,7 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] - m_AddedGameObjects: - - targetCorrespondingSourceObject: {fileID: 3032075919872812610, guid: ec5007446d8bbca48bf02b3a7e752da1, type: 3} - insertIndex: -1 - addedObject: {fileID: 4910321061857220296} + m_AddedGameObjects: [] m_AddedComponents: - targetCorrespondingSourceObject: {fileID: 809911508645467208, guid: ec5007446d8bbca48bf02b3a7e752da1, type: 3} insertIndex: -1 @@ -3684,11 +3681,6 @@ PrefabInstance: insertIndex: -1 addedObject: {fileID: 1683288374126251336} m_SourcePrefab: {fileID: 100100000, guid: ec5007446d8bbca48bf02b3a7e752da1, type: 3} ---- !u!4 &1683288374126251322 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 3032075919872812610, guid: ec5007446d8bbca48bf02b3a7e752da1, type: 3} - m_PrefabInstance: {fileID: 1683288374126251321} - m_PrefabAsset: {fileID: 0} --- !u!114 &1683288374126251323 MonoBehaviour: m_ObjectHideFlags: 0 @@ -3869,7 +3861,7 @@ PrefabInstance: serializedVersion: 2 m_Modification: serializedVersion: 3 - m_TransformParent: {fileID: 1683288374126251322} + m_TransformParent: {fileID: 0} m_Modifications: - target: {fileID: 144529238244638330, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} propertyPath: m_LocalPosition.x @@ -3911,6 +3903,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 2894273998966960381, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} + propertyPath: GlobalObjectIdHash + value: 3972580189 + objectReference: {fileID: 0} - target: {fileID: 5991265243222894942, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} propertyPath: m_Name value: Rope @@ -3920,11 +3916,6 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} ---- !u!4 &4910321061857220296 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 144529238244638330, guid: 0248db69242a3dd47898c6742b6c9f60, type: 3} - m_PrefabInstance: {fileID: 4910321061857220295} - m_PrefabAsset: {fileID: 0} --- !u!1001 &5796191506433166633 PrefabInstance: m_ObjectHideFlags: 0 @@ -4799,6 +4790,7 @@ SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 1683288374126251321} + - {fileID: 4910321061857220295} - {fileID: 1536317696} - {fileID: 4158834148864819266} - {fileID: 1600052934} diff --git a/Assets/Scenes/MultiplayerTestingScenes/Multi-SetupLoad.unity b/Assets/Scenes/MultiplayerTestingScenes/Multi-SetupLoad.unity index 46957f4..a7969e8 100644 --- a/Assets/Scenes/MultiplayerTestingScenes/Multi-SetupLoad.unity +++ b/Assets/Scenes/MultiplayerTestingScenes/Multi-SetupLoad.unity @@ -13,7 +13,7 @@ OcclusionCullingSettings: --- !u!104 &2 RenderSettings: m_ObjectHideFlags: 0 - serializedVersion: 9 + serializedVersion: 10 m_Fog: 0 m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} m_FogMode: 3 @@ -44,7 +44,6 @@ RenderSettings: LightmapSettings: m_ObjectHideFlags: 0 serializedVersion: 12 - m_GIWorkflowMode: 1 m_GISettings: serializedVersion: 2 m_BounceScale: 1 @@ -67,9 +66,6 @@ LightmapSettings: m_LightmapParameters: {fileID: 0} m_LightmapsBakeMode: 1 m_TextureCompression: 1 - m_FinalGather: 0 - m_FinalGatherFiltering: 1 - m_FinalGatherRayCount: 256 m_ReflectionCompression: 2 m_MixedBakeMode: 2 m_BakeBackend: 1 @@ -97,7 +93,7 @@ LightmapSettings: m_ExportTrainingData: 0 m_TrainingDataDestination: TrainingData m_LightProbeSampleCountMultiplier: 4 - m_LightingDataAsset: {fileID: 0} + m_LightingDataAsset: {fileID: 20201, guid: 0000000000000000f000000000000000, type: 0} m_LightingSettings: {fileID: 0} --- !u!196 &4 NavMeshSettings: @@ -464,15 +460,17 @@ MonoBehaviour: m_lineSpacingMax: 0 m_paragraphSpacing: 0 m_charWidthMaxAdj: 0 - m_enableWordWrapping: 1 + m_TextWrappingMode: 1 m_wordWrappingRatios: 0.4 m_overflowMode: 0 m_linkedTextComponent: {fileID: 0} parentLinkedComponent: {fileID: 0} m_enableKerning: 1 + m_ActiveFontFeatures: 6e72656b m_enableExtraPadding: 0 checkPaddingRequired: 0 m_isRichText: 1 + m_EmojiFallbackSupport: 1 m_parseCtrlCharacters: 1 m_isOrthographic: 1 m_isCullingEnabled: 0 @@ -679,15 +677,17 @@ MonoBehaviour: m_lineSpacingMax: 0 m_paragraphSpacing: 0 m_charWidthMaxAdj: 0 - m_enableWordWrapping: 1 + m_TextWrappingMode: 1 m_wordWrappingRatios: 0.4 m_overflowMode: 0 m_linkedTextComponent: {fileID: 0} parentLinkedComponent: {fileID: 0} m_enableKerning: 1 + m_ActiveFontFeatures: 00000000 m_enableExtraPadding: 0 checkPaddingRequired: 0 m_isRichText: 1 + m_EmojiFallbackSupport: 1 m_parseCtrlCharacters: 1 m_isOrthographic: 1 m_isCullingEnabled: 0 @@ -952,15 +952,17 @@ MonoBehaviour: m_lineSpacingMax: 0 m_paragraphSpacing: 0 m_charWidthMaxAdj: 0 - m_enableWordWrapping: 1 + m_TextWrappingMode: 1 m_wordWrappingRatios: 0.4 m_overflowMode: 0 m_linkedTextComponent: {fileID: 0} parentLinkedComponent: {fileID: 0} m_enableKerning: 1 + m_ActiveFontFeatures: 6e72656b m_enableExtraPadding: 0 checkPaddingRequired: 0 m_isRichText: 1 + m_EmojiFallbackSupport: 1 m_parseCtrlCharacters: 1 m_isOrthographic: 1 m_isCullingEnabled: 0 @@ -1270,9 +1272,8 @@ Light: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1886719419} m_Enabled: 1 - serializedVersion: 10 + serializedVersion: 11 m_Type: 1 - m_Shape: 0 m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} m_Intensity: 1 m_Range: 10 diff --git a/Assets/Scripts/Rope/Rope.cs b/Assets/Scripts/Rope/Rope.cs index 432085c..2699a0f 100644 --- a/Assets/Scripts/Rope/Rope.cs +++ b/Assets/Scripts/Rope/Rope.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; +[System.Serializable] public class Rope { public List points { get ; private set; } diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 4b94412..e2df954 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -54,6 +54,7 @@ public class RopeSimulator : NetworkBehaviour public float ropeMaxLength, ropeMinLength; [Header("Rope Colliders")] + [SerializeField] GameObject colliderPrefab; [SerializeField] string colliderTag = "Rope"; [SerializeField] Transform ropeCollidersParent; @@ -254,30 +255,44 @@ public class RopeSimulator : NetworkBehaviour foreach (var point in rope.points) { - GameObject ropeCollider = new GameObject("Rope Collider"); - ropeCollider.tag = colliderTag; - ropeCollider.transform.parent = ropeCollidersParent; + GameObject ropeCollider = Instantiate(colliderPrefab); + var ropeNO = ropeCollider.GetComponent(); + ropeNO.Spawn(); + + ropeNO.TrySetParent(ropeCollidersParent); ropeCollider.transform.position = point.position; + ropeCollider.tag = colliderTag; ropeCollider.layer = LayerMask.NameToLayer("Rope"); - if (!IsClient) // Only coliders for the server/host - { - var colliderComponent = ropeCollider.AddComponent(); - colliderComponent.radius = ropeRadius; - } - - - var rigidBody = ropeCollider.AddComponent(); + ropeCollider.GetComponent().radius = ropeRadius; + var rigidBody = ropeCollider.GetComponent(); rigidBody.isKinematic = true; + } + Debug.Break(); + } + + private void DrawRope() + { + // Update line renderer + List positions = new List(ropeCollidersParent.childCount); + for (int i = 0; i < ropeCollidersParent.childCount; i++) + { + positions.Add(ropeCollidersParent.GetChild(i).position); + } + lineRenderer.positionCount = positions.Count; + lineRenderer.SetPositions(positions.ToArray()); } private void Update() { - if (!IsInitialized) - return; - if (!ShouldSimulate) + { + DrawRope(); + return; + } + + if (!IsInitialized) return; colliderToSquezeForce.Clear(); @@ -319,11 +334,6 @@ public class RopeSimulator : NetworkBehaviour squezeDamageReceiver.TakeSquezeDamage(collider.Value * squezeDamage * swingMultiplier); } - // Update line renderer - var positions = rope.points.Select(p => new Vector3(p.position.x, p.position.y, 0f)).ToArray(); - lineRenderer.positionCount = positions.Length; - lineRenderer.SetPositions(positions); - // Handle xy dampening on z gravity foreach (var point in rope.points) { @@ -332,6 +342,8 @@ public class RopeSimulator : NetworkBehaviour Vector2 newXYPos = Vector2.MoveTowards(new Vector2(point.position.x, point.position.y), new Vector2(point.prevPosition.x, point.prevPosition.y), Mathf.Abs(point.position.z * xyGravityDampScalor)); point.position.Set(newXYPos.x, newXYPos.y, 0f); } + + DrawRope(); } private void PlayerPullAnimation(float overshoot)