Advertisement
Guest User

Untitled

a guest
Sep 27th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 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> receivedStates = new List<RopeState>();
  11.  
  12. [System.Serializable]
  13. public struct RopeState
  14. {
  15. public Vector4 position;
  16. public Vector4 velocity;
  17. }
  18.  
  19. private void Start()
  20. {
  21. rope = GetComponent<ObiRope>();
  22. }
  23.  
  24. private void FixedUpdate()
  25. {
  26. if (IsServer)
  27. {
  28. // Server sends rope states (positions and velocities) to clients.
  29. receivedStates.Clear();
  30. for (int i = 0; i < rope.particleCount; i++)
  31. {
  32. Vector4 position = rope.solver.positions[rope.GetParticleRuntimeIndex(i)];
  33. Vector4 velocity = rope.solver.velocities[rope.GetParticleRuntimeIndex(i)];
  34. receivedStates.Add(new RopeState { position = position, velocity = velocity });
  35. }
  36. // Call a client RPC to sync the rope states.
  37. syncRopeClient(receivedStates);
  38. }
  39. }
  40.  
  41. // This method is now a client RPC.
  42. [Client]
  43. private void syncRopeClient(List<RopeState> states)
  44. {
  45. if (!IsServer)
  46. {
  47. // Client receives rope states (positions and velocities) from the server.
  48. for (int i = 0; i < rope.particleCount; i++)
  49. {
  50. Vector4 targetPosition = states[i].position;
  51. Vector4 currentPosition = rope.solver.positions[rope.GetParticleRuntimeIndex(i)];
  52.  
  53. rope.solver.invMasses[rope.GetParticleRuntimeIndex(i)] = 0;
  54.  
  55. rope.solver.positions[rope.GetParticleRuntimeIndex(i)] = states[i].position;
  56.  
  57. // Apply the received velocity to the rope's solver.
  58. rope.solver.velocities[rope.GetParticleRuntimeIndex(i)] = states[i].velocity;
  59.  
  60. rope.solver.UpdateBackend();
  61. }
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement