Search Unity

Sync transform of non PhotonNetwork.Instantiated objects smoothly. Please Help!

Discussion in 'Scripting' started by Red_Kay, Nov 27, 2015.

  1. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    Hello folks,

    I am making an online multiplayer football game. In the game there's a ball already in the scene (balli s not instantiated it is present in scene when the game starts) and it has a Photon View and a script which syncs the transform of the ball and this script is observed.

    [This is that script]

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ball_SyncTransform : Photon.MonoBehaviour {
    5.  
    6.     Vector3 position;
    7.     Quaternion rotation;
    8.     [SerializeField]
    9.     float smoothing;
    10.  
    11.     void FixedUpdate()
    12.     {
    13.         if (GameObject.FindGameObjectWithTag(Tags.networkManager).GetComponent<Networkmanager>().gameHasBegan)
    14.         {
    15.             if ( !photonView.isMine)
    16.             {
    17.                 transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
    18.                 transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
    19.             }
    20.         }
    21.        
    22.        
    23.     }
    24.  
    25.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    26.     {
    27.         if (stream.isWriting)
    28.         {
    29.             stream.SendNext(transform.position);
    30.             stream.SendNext(transform.rotation);
    31.         }
    32.         else if (stream.isReading)
    33.         {
    34.             position = (Vector3)stream.ReceiveNext();
    35.             rotation = (Quaternion)stream.ReceiveNext();
    36.         }
    37.     }
    38. }
    39.  
    When the ball changes its transform it appears smoothly on the Master Client but it lags very much on other clients please help!

    Thanks for your time! :)
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    First off, line 13 needs to be changed big time, you are using GameObject.Find and on top of that you are using GetComponent, this is very poor and performance intensive especially since it is getting called as fast as the hardware and go. FixedUpdate should only ever be used for Physics and nothing else use Update for syncing.

    For the lag problem use 0.1F as the Lerp instead of the smoothing variable.

    transform.position=Vector3.Lerp(transform.position, position, 0.1F);

    ^^ this should fix your problem
     
  3. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94


    Watch the video please. Now I have done as you said but still when the client takes the ball and looks around theres a bit choppyness in the ball. actually theres a circle trigger in front of the player which detects if "Ball" collided or not, if it did than the ball will become the parent of the "BallPosition" gameObject. Please watch the video to understand
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    So in this case you don't want to update the position of the ball if it is parented to something. As you can see, when the ball was in the 'arena' it was not choppy but very fast movement when the player rotated was causing some choppiness. In Photon there are RPC calls you can make. These RPCs are events you can have the Ball have an event when it has been 'picked' up and who the 'owner is' when this happens it will no longer update its position as it no longer needs to as it is being physically moved by the player.

    Football.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(PhotonView))]
    5. public class Football : MonoBehaviour {
    6.     PhotonView m_view;
    7.     Vector3 m_networkPosition;
    8.     Quaternion m_networkRotation;
    9.     bool m_isParented;
    10.     int m_holderID;
    11.  
    12.     protected virtual void Awake() {
    13.         this.m_view = this.GetComponent<PhotonView>();
    14.     }
    15.  
    16.     protected virtual void Update() {
    17.         if(!this.m_view.isMine && ! this.m_isParented) {
    18.             this.transform.position = Vector3.Lerp(this.transform.position, this.m_networkPosition, 0.1F);
    19.             this.transform.rotation = Quaternion.Lerp(this.transform.rotation, this.m_networkRotation, 0.1F);
    20.         }
    21.     }
    22.  
    23.     [PunRPC]
    24.     protected virtual void RequestPickUp(int viewID) {
    25.         if(this.m_holderID != -1) return;
    26.  
    27.         PhotonView view = PhotonView.Find(viewID);
    28.  
    29.         if(view != null) {
    30.             this.m_isParented = true;
    31.             this.transform.parent = view.transform;
    32.             this.transform.localPosition = Vector3.zero;
    33.             this.transform.rotation = Quaternion.identity;
    34.             this.m_holderID = view.owner.ID;
    35.         }
    36.     }
    37.  
    38.     [PunRPC]
    39.     protected virtual void RequestDrop() {
    40.         this.transform.parent = null;
    41.         this.m_isParented = false;
    42.         this.m_holderID = -1;
    43.     }
    44.  
    45.     protected void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    46.         if(stream.isWriting) {
    47.             stream.SendNext(this.transform.position);
    48.             stream.SendNext(this.transform.rotation);
    49.         } else {
    50.             this.m_networkPosition = (Vector3)stream.ReceiveNext();
    51.             this.m_networkRotation = (Quaternion)stream.ReceiveNext();
    52.         }
    53.     }
    54.  
    55.     protected virtual void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer) {
    56.         if(this.m_holderID == otherPlayer.ID) {
    57.             this.m_view.RPC("RequestDrop", PhotonTargets.AllBufferedViaServer);
    58.         }
    59.     }
    60.  
    61.     public virtual PhotonView View {
    62.         get {
    63.             return this.m_view;
    64.         }
    65.     }
    66. }
    67.  
    then assuming we have a reference from the ball we can call the RPC as so...

    Code (CSharp):
    1. ball.View.RPC("RequestPickUp", PhotonTargets.AllBufferedViaServer, this.m_view.viewID);
     
    Red_Kay likes this.
  5. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    In line 31 view.transform is of the parent to whom the ball is going to be the child right?

    And where should I call : "ball.View.RPC("RequestPickUp", PhotonTargets.AllBufferedViaServer, this.m_view.viewID);"

    [UPDATED]
    How should I mark your previous reply as "ANSWER"

    Thank you sooo much for your time! :)
     
    Last edited: Nov 28, 2015
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    This should be for the player when it walks ontop of the ball. You should check to see if you can get a reference to the Football script and then use the View property to call the RPC function and then pass the player's viewID to it.
     
  7. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    Hello sorry for not responding kinda getting pissed off from these lags.

    Everything is fine except the ball synchronization.

     
  8. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    Is there any need to transfer ownership of the ball??