No need for rope collider and rigidbody on rope points

This commit is contained in:
Sveske_Juice 2024-03-17 13:55:28 +01:00
parent ad1746d4a2
commit 2b6f6606fc
3 changed files with 34 additions and 88 deletions

View File

@ -9,8 +9,6 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 3817028170077760731}
- component: {fileID: 4590407122831754579}
- component: {fileID: 8963967235500975272}
m_Layer: 0
m_Name: RopePoint
m_TagString: Untagged
@ -33,66 +31,3 @@ Transform:
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: 128
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

View File

@ -9,7 +9,8 @@
"GUID:c0e1b40f519e6e84b8f4af9930403ecb",
"GUID:3b8ed52f1b5c64994af4c4e0aa4b6c4b",
"GUID:1491147abca9d7d4bb7105af628b223e",
"GUID:068707ae079d6e851b7a76adaa3014f8"
"GUID:068707ae079d6e851b7a76adaa3014f8",
"GUID:f0aa6e1ec272fc041a727a9dfb7c1e67"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -4,6 +4,8 @@ using System.Linq;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Assertions;
using Utilities;
using UnityUtils;
public class RopeSimulator : NetworkBehaviour
{
@ -65,9 +67,10 @@ public class RopeSimulator : NetworkBehaviour
[Header("Netcode")]
private const int k_rngSeed = 6969;
private const float k_sendRopeDataRPCThreshold = 0.1f;
public float k_ropeReconciliateThreshold = 8f;
private const float k_sendRopeDataDelay = 8f;
public float k_ropeReconciliateThreshold = 10f;
private System.Random rng = new System.Random(k_rngSeed);
private CountdownTimer ropeSendTimer;
private int[] order;
@ -92,6 +95,10 @@ public class RopeSimulator : NetworkBehaviour
{
Destroy(instance);
}
ropeSendTimer = new(k_sendRopeDataDelay);
// ropeSendTimer.OnTimerStop += SendRopeData;
ropeSendTimer.Start();
}
private void OnEnable()
@ -256,11 +263,6 @@ public class RopeSimulator : NetworkBehaviour
ropeCollider.transform.position = point.position;
ropeCollider.tag = colliderTag;
ropeCollider.layer = LayerMask.NameToLayer("Rope");
ropeCollider.GetComponent<CircleCollider2D>().radius = ropeRadius;
var rigidBody = ropeCollider.GetComponent<Rigidbody2D>();
rigidBody.isKinematic = true;
}
}
@ -283,13 +285,26 @@ public class RopeSimulator : NetworkBehaviour
Rope serverRope = Rope.FromNetworkRope(nrope, distBetweenRopePoints);
float diff = Rope.CalcDiff(this.rope, serverRope);
Debug.Log(diff);
if (diff > k_ropeReconciliateThreshold)
{
Debug.Log($"server client rope diff: {diff}");
// if (diff > k_ropeReconciliateThreshold)
// {
// Debug.LogWarning("Reconciliating rope!");
// this.rope = serverRope;
// Debug.Log(Rope.CalcDiff(this.rope, serverRope));
// }
}
private void SendRopeData()
{
if (!IsServer) return;
Debug.Log($"Sending rope to client");
NetworkRope nrope = Rope.ToNetworkRope(this.rope);
// Send server rope to client for reconciliation
ServerRopeDataReceivedRpc(nrope);
ropeSendTimer.Reset();
ropeSendTimer.Start();
}
private void Update()
@ -303,14 +318,8 @@ public class RopeSimulator : NetworkBehaviour
rope.points.Last().position = end.position;
float ropeDiff = Simulate(Time.fixedDeltaTime);
if (IsServer)
{
Debug.Log($"Sending rope to client");
NetworkRope nrope = Rope.ToNetworkRope(this.rope);
// Send server rope to client for reconciliation
ServerRopeDataReceivedRpc(nrope);
}
ropeSendTimer.Tick(Time.deltaTime);
Debug.Log(ropeSendTimer.Progress);
// Update the rope collider positions
for (int i = 0; i < rope.points.Length; i++)
@ -354,6 +363,7 @@ public class RopeSimulator : NetworkBehaviour
}
DrawRope();
SendRopeData();
}
private void PlayerPullAnimation(float overshoot)