So I have made a game in which you can play online.
My problem is that when I want to sync a ball, the server can push it correctly, but the client cannot push it.
I have a attempted a RPC call btw.
Sorry if I’m not explaining correctly or if I’m missing things you need to know, just ask me whatever you want and I’ll answer it.
My ball code:
using UnityEngine;
using System.Collections;
public class NetworkSync : MonoBehaviour {
private Rigidbody rb;
private float lastSynchronizationTime = 0;
private float syncDelay = 0;
private float syncTime = 0;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private Vector3 syncStartRotation = Vector3.zero;
private Vector3 syncEndRotation = Vector3.zero;
private NetworkView nv;
void Awake()
{
rb = GetComponent<Rigidbody>();
nv = GetComponent<NetworkView>();
}
void OnCollisionEnter(Collision collision) {
if(!GetComponent<NetworkView>().isMine)
{
nv.RPC("DoPush", RPCMode.Server, collision.relativeVelocity);
}
}
void Update()
{
if(!GetComponent<NetworkView>().isMine)
{
SyncedMovement();
}
}
private void SyncedMovement()
{
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
transform.rotation = Quaternion.Euler(Vector3.Lerp(syncStartRotation, syncEndRotation, syncTime / syncDelay));
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
Vector3 syncRotation = Vector3.zero;
Vector3 syncRotVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rb.position;
stream.Serialize(ref syncPosition);
syncRotVelocity = rb.angularVelocity;
stream.Serialize(ref syncRotVelocity);
syncVelocity = rb.velocity;
stream.Serialize(ref syncVelocity);
syncRotation = rb.rotation.eulerAngles;
stream.Serialize(ref syncPosition);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
stream.Serialize(ref syncRotation);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rb.position;
syncEndRotation = syncRotation + syncRotVelocity * syncDelay;
syncStartRotation = rb.rotation.eulerAngles;
}
}
[RPC]
void DoPush(Vector3 push)
{
Debug.Log(push);
rb.AddForce(push);
}
}
Thanks for the help!