Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to sync up rotation... help?

Discussion in 'Multiplayer' started by Testpig, Dec 26, 2014.

  1. Testpig

    Testpig

    Joined:
    Aug 7, 2013
    Posts:
    6
    So i used what i learned from your Merry Fragmas m-player tutorials, and used it as the bases to get my little learning game to work.

    The issue i am having is that the players rotation is not syncing over. View video to see what is happening to me in game.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerNetworkMover : Photon.MonoBehaviour {
    5.  
    6.     public delegate void Respawn(float time);
    7.     public event Respawn RespawnMe;
    8.  
    9.     Vector3 position;
    10.     Quaternion rotation;
    11.     float smoothing = 10f;
    12.     float health = 100f;
    13.     bool idle = false;
    14.     bool run = false;
    15.     bool initialLoad = true;
    16.  
    17.     Animator anim;
    18.  
    19.     void Start ()
    20.     {
    21.         anim = GetComponentInChildren<Animator> ();
    22.         if(photonView.isMine)
    23.         {
    24.             GetComponent<AxisPlayerMove>().enabled = true;
    25.             GetComponentInChildren<ARPGCameraController>().enabled = true;
    26.             foreach(Camera cam in GetComponentsInChildren<Camera>())
    27.                 cam.enabled = true;
    28.         }
    29.         else{
    30.             StartCoroutine("UpdateData");
    31.         }
    32.     }
    33.  
    34.     IEnumerator UpdateData()
    35.     {
    36.         if(initialLoad)
    37.         {
    38.             initialLoad = false;
    39.             transform.position = position;
    40.             transform.rotation = rotation;
    41.         }
    42.         while(true)
    43.         {
    44.             transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
    45.             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
    46.             anim.SetBool ("idle", idle);
    47.             anim.SetBool ("run", run);
    48.             yield return null;
    49.         }
    50.     }
    51.  
    52.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    53.     {
    54.         if(stream.isWriting)
    55.         {
    56.             stream.SendNext(transform.position);
    57.             stream.SendNext(transform.rotation);
    58.             stream.SendNext(health);
    59.             stream.SendNext(anim.GetBool ("idle"));
    60.             stream.SendNext(anim.GetBool ("run"));
    61.         }
    62.         else
    63.         {
    64.             position = (Vector3)stream.ReceiveNext();
    65.             rotation = (Quaternion)stream.ReceiveNext();
    66.             health = (float)stream.ReceiveNext();
    67.             idle = (bool)stream.ReceiveNext();
    68.             run = (bool)stream.ReceiveNext();
    69.         }
    70.     }
    71.  
    72.     [RPC]
    73.     public void GetShot(float damage)
    74.     {
    75.         health -= damage;
    76.         if(health <= 0 && photonView.isMine)
    77.         {
    78.             if(RespawnMe != null)
    79.                 RespawnMe(3f);
    80.          
    81.             PhotonNetwork.Destroy (gameObject);
    82.         }
    83.     }
    84.  
    85. }
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Don't think its the best practice to run the update in a coroutine. Why not run it in Update()? At least only run it once per frame by using the yield wait for end of frame method. But still think you should move it to Update().

    The Lerp seems fine though, not sure if it is the coroutine that somehow F***s it up.
     
  3. Testpig

    Testpig

    Joined:
    Aug 7, 2013
    Posts:
    6
    would the issue have anything to do with, the original character controllers?
     
  4. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    That could very well be. Dont know what scripts you got running.... Remember to disable all movement/rotation code on views that do not belong to you.
     
  5. Testpig

    Testpig

    Joined:
    Aug 7, 2013
    Posts:
    6
    yea it had something to do with my controller. ugh.

    switched the controller to another set up, and it rotates just fine. a bit choppy, but works none the less!