Search Unity

Networking a Ball Rigidbody so both players can push it

Discussion in 'Scripting' started by Crack498, Apr 25, 2015.

  1. Crack498

    Crack498

    Joined:
    Apr 25, 2015
    Posts:
    3
    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:
    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class NetworkSync : MonoBehaviour {
    7.  
    8. private Rigidbody rb;
    9.     private float lastSynchronizationTime = 0;
    10.     private float syncDelay = 0;
    11.     private float syncTime = 0;
    12.     private Vector3 syncStartPosition = Vector3.zero;
    13.     private Vector3 syncEndPosition = Vector3.zero;
    14.     private Vector3 syncStartRotation = Vector3.zero;
    15.     private Vector3 syncEndRotation = Vector3.zero;
    16.  
    17.     private NetworkView nv;
    18.  
    19.     void Awake()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         nv = GetComponent<NetworkView>();
    23.     }
    24.  
    25.     void OnCollisionEnter(Collision collision) {
    26.         if(!GetComponent<NetworkView>().isMine)
    27.         {
    28.             nv.RPC("DoPush", RPCMode.Server, collision.relativeVelocity);
    29.         }
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         if(!GetComponent<NetworkView>().isMine)
    35.         {
    36.             SyncedMovement();
    37.         }
    38.     }
    39.  
    40.     private void SyncedMovement()
    41.     {
    42.         syncTime += Time.deltaTime;
    43.         transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
    44.         transform.rotation = Quaternion.Euler(Vector3.Lerp(syncStartRotation, syncEndRotation, syncTime / syncDelay));
    45.     }
    46.  
    47.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    48.     {
    49.         Vector3 syncPosition = Vector3.zero;
    50.         Vector3 syncVelocity = Vector3.zero;
    51.         Vector3 syncRotation = Vector3.zero;
    52.         Vector3 syncRotVelocity = Vector3.zero;
    53.         if (stream.isWriting)
    54.         {
    55.             syncPosition = rb.position;
    56.             stream.Serialize(ref syncPosition);
    57.  
    58.             syncRotVelocity = rb.angularVelocity;
    59.             stream.Serialize(ref syncRotVelocity);
    60.  
    61.             syncVelocity = rb.velocity;
    62.             stream.Serialize(ref syncVelocity);
    63.  
    64.             syncRotation = rb.rotation.eulerAngles;
    65.             stream.Serialize(ref syncPosition);
    66.         }
    67.         else
    68.         {
    69.             stream.Serialize(ref syncPosition);
    70.             stream.Serialize(ref syncVelocity);
    71.             stream.Serialize(ref syncRotation);
    72.          
    73.             syncTime = 0f;
    74.             syncDelay = Time.time - lastSynchronizationTime;
    75.             lastSynchronizationTime = Time.time;
    76.          
    77.             syncEndPosition = syncPosition + syncVelocity * syncDelay;
    78.             syncStartPosition = rb.position;
    79.  
    80.             syncEndRotation = syncRotation + syncRotVelocity * syncDelay;
    81.             syncStartRotation = rb.rotation.eulerAngles;
    82.         }
    83.     }
    84.  
    85.     [RPC]
    86.     void DoPush(Vector3 push)
    87.     {
    88.         Debug.Log(push);
    89.         rb.AddForce(push);
    90.     }
    91.  
    92. }
    93.  
    Thanks for the help!
     
    Last edited: Apr 27, 2015
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    rb is outside of the class, how does this work effectively at all?
     
  3. Crack498

    Crack498

    Joined:
    Apr 25, 2015
    Posts:
    3
    What? I have it inside the class. It must have been some copy-paste error, sorry :confused:
     
  4. Crack498

    Crack498

    Joined:
    Apr 25, 2015
    Posts:
    3
    I tried to send a RPC call to the ball, adding a force to it to the server when the other client pushes it but doesn't work as expected.
     
  5. ZhavShaw

    ZhavShaw

    Joined:
    Aug 12, 2013
    Posts:
    173
    Can anyone shed some light on this?
     
  6. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Can you explain more your problem ?

    I do not "see" what is the problem.

    When the client collide the ball, the RPC is not raised ?
     
  7. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Carrefull here, you are writing 4 properties and only reading 3