Advertisement
Guest User

Untitled

a guest
Sep 27th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Obi;
  4. using FishNet.Object;
  5. using FishNet.Object.Synchronizing;
  6.  
  7. public class RopeSync : NetworkBehaviour
  8. {
  9.     ObiRope rope;
  10.     List<RopeState> ServerSendStates = new List<RopeState>();
  11.     List<RopeState> ClientRecievedStates = new List<RopeState>();
  12.  
  13.     [System.Serializable]
  14.     public struct RopeState
  15.     {
  16.         public Vector4 position;
  17.         public Vector4 velocity;
  18.         public Vector4 externalForces;
  19.         public Vector4 externalTorques;
  20.     }
  21.  
  22.     public void OnEnable()
  23.     {
  24.         if (IsClient)
  25.         {
  26.             GetComponent<ObiActor>().OnEndStep += EndStep;
  27.         }
  28.     }
  29.  
  30.     public void OnDisable()
  31.     {
  32.         if (IsClient)
  33.         {
  34.             GetComponent<ObiActor>().OnEndStep -= EndStep;
  35.         }
  36.     }
  37.  
  38.     void EndStep(ObiActor actor, float stepTime)
  39.     {
  40.         //Update the server values
  41.         if (IsServer)
  42.         {
  43.             // Server sends rope states (positions, velocities, forces, and torques) to clients.
  44.             ServerSendStates.Clear();
  45.             for (int i = 0; i < rope.particleCount; i++)
  46.             {
  47.                 Vector4 position = rope.solver.positions[rope.GetParticleRuntimeIndex(i)];
  48.                 Vector4 velocity = rope.solver.velocities[rope.GetParticleRuntimeIndex(i)];
  49.                 Vector4 externalForces = rope.solver.externalForces[rope.GetParticleRuntimeIndex(i)];
  50.                 Vector4 externalTorques = rope.solver.externalTorques[rope.GetParticleRuntimeIndex(i)];
  51.  
  52.                 ServerSendStates.Add(new RopeState
  53.                 {
  54.                     position = position,
  55.                     velocity = velocity,
  56.                     externalForces = externalForces,
  57.                     externalTorques = externalTorques
  58.                 });
  59.             }
  60.             // Call a client RPC to sync the rope states.
  61.             syncRopeClient(ServerSendStates);
  62.         }
  63.  
  64.         if (ClientRecievedStates != null)
  65.         {
  66.             // Client receives rope states (positions, velocities, forces, and torques) from the server.
  67.             for (int i = 0; i < rope.particleCount; i++)
  68.             {
  69.                 rope.solver.externalForces[rope.GetParticleRuntimeIndex(i)] = ClientRecievedStates[i].externalForces;
  70.                 rope.solver.externalTorques[rope.GetParticleRuntimeIndex(i)] = ClientRecievedStates[i].externalTorques;
  71.                 rope.solver.invMasses[rope.GetParticleRuntimeIndex(i)] = 0f; //remove all mass on client
  72.                 rope.solver.invRotationalMasses[rope.GetParticleRuntimeIndex(i)] = 0f;
  73.  
  74.             }
  75.             //  rope.solver.PushSolverParameters();
  76.             //  rope.solver.UpdateBackend();
  77.  
  78.             ClientRecievedStates.Clear();
  79.         }
  80.     }
  81.     private void Start()
  82.     {
  83.         rope = GetComponent<ObiRope>();
  84.  
  85.     }
  86.     // This method is now a client RPC.
  87.     [Client]
  88.     private void syncRopeClient(List<RopeState> states)
  89.     {
  90.         if (!IsServer)
  91.         {
  92.  
  93.             if (ClientRecievedStates.Count == 0)
  94.             {
  95.  
  96.                 ClientRecievedStates = states;
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement