Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using Obi;
- using FishNet.Object;
- using FishNet.Object.Synchronizing;
- public class RopeSync : NetworkBehaviour
- {
- ObiRope rope;
- List<RopeState> receivedStates = new List<RopeState>();
- [System.Serializable]
- public struct RopeState
- {
- public Vector4 position;
- public Vector4 velocity;
- }
- private void Start()
- {
- rope = GetComponent<ObiRope>();
- }
- private void FixedUpdate()
- {
- if (IsServer)
- {
- // Server sends rope states (positions and velocities) to clients.
- receivedStates.Clear();
- for (int i = 0; i < rope.particleCount; i++)
- {
- Vector4 position = rope.solver.positions[rope.GetParticleRuntimeIndex(i)];
- Vector4 velocity = rope.solver.velocities[rope.GetParticleRuntimeIndex(i)];
- receivedStates.Add(new RopeState { position = position, velocity = velocity });
- }
- // Call a client RPC to sync the rope states.
- syncRopeClient(receivedStates);
- }
- }
- // This method is now a client RPC.
- [Client]
- private void syncRopeClient(List<RopeState> states)
- {
- if (!IsServer)
- {
- // Client receives rope states (positions and velocities) from the server.
- for (int i = 0; i < rope.particleCount; i++)
- {
- Vector4 targetPosition = states[i].position;
- Vector4 currentPosition = rope.solver.positions[rope.GetParticleRuntimeIndex(i)];
- rope.solver.invMasses[rope.GetParticleRuntimeIndex(i)] = 0;
- rope.solver.positions[rope.GetParticleRuntimeIndex(i)] = states[i].position;
- // Apply the received velocity to the rope's solver.
- rope.solver.velocities[rope.GetParticleRuntimeIndex(i)] = states[i].velocity;
- rope.solver.UpdateBackend();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement