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> ServerSendStates = new List<RopeState>();
- List<RopeState> ClientRecievedStates = new List<RopeState>();
- [System.Serializable]
- public struct RopeState
- {
- public Vector4 position;
- public Vector4 velocity;
- public Vector4 externalForces;
- public Vector4 externalTorques;
- }
- public void OnEnable()
- {
- if (IsClient)
- {
- GetComponent<ObiActor>().OnEndStep += EndStep;
- }
- }
- public void OnDisable()
- {
- if (IsClient)
- {
- GetComponent<ObiActor>().OnEndStep -= EndStep;
- }
- }
- void EndStep(ObiActor actor, float stepTime)
- {
- //Update the server values
- if (IsServer)
- {
- // Server sends rope states (positions, velocities, forces, and torques) to clients.
- ServerSendStates.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)];
- Vector4 externalForces = rope.solver.externalForces[rope.GetParticleRuntimeIndex(i)];
- Vector4 externalTorques = rope.solver.externalTorques[rope.GetParticleRuntimeIndex(i)];
- ServerSendStates.Add(new RopeState
- {
- position = position,
- velocity = velocity,
- externalForces = externalForces,
- externalTorques = externalTorques
- });
- }
- // Call a client RPC to sync the rope states.
- syncRopeClient(ServerSendStates);
- }
- if (ClientRecievedStates != null)
- {
- // Client receives rope states (positions, velocities, forces, and torques) from the server.
- for (int i = 0; i < rope.particleCount; i++)
- {
- rope.solver.externalForces[rope.GetParticleRuntimeIndex(i)] = ClientRecievedStates[i].externalForces;
- rope.solver.externalTorques[rope.GetParticleRuntimeIndex(i)] = ClientRecievedStates[i].externalTorques;
- rope.solver.invMasses[rope.GetParticleRuntimeIndex(i)] = 0f; //remove all mass on client
- rope.solver.invRotationalMasses[rope.GetParticleRuntimeIndex(i)] = 0f;
- }
- // rope.solver.PushSolverParameters();
- // rope.solver.UpdateBackend();
- ClientRecievedStates.Clear();
- }
- }
- private void Start()
- {
- rope = GetComponent<ObiRope>();
- }
- // This method is now a client RPC.
- [Client]
- private void syncRopeClient(List<RopeState> states)
- {
- if (!IsServer)
- {
- if (ClientRecievedStates.Count == 0)
- {
- ClientRecievedStates = states;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement