Search Unity

Why is this sync rotation script not working at Spawn

Discussion in 'Multiplayer' started by pKallv, Sep 20, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    From what i seems to remember UNET does not automatically spawn with rotation. I have the following script attached to my objects for rotation:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Object_SyncRotation : NetworkBehaviour {
    6.  
    7.     private Transform myTransform;
    8.  
    9.     // Object rotation vars
    10.     [SyncVar] private Quaternion syncRot;
    11.     public float rotationLerpRate = 15;
    12.  
    13.     private Quaternion lastRot;
    14.     private float threshold = 5f;
    15.  
    16.  
    17.     void Start() {
    18.         myTransform = GetComponent<Transform> ();
    19.     }
    20.  
    21.     void FixedUpdate () {
    22.         TransmitRotation ();
    23.         LerpRotation ();
    24.     }
    25.  
    26.     void LerpRotation() {
    27.         if (!hasAuthority) {
    28.             myTransform.rotation = Quaternion.Lerp (myTransform.rotation, syncRot, Time.deltaTime * rotationLerpRate);
    29.         }
    30.     }
    31.  
    32.     [Command]
    33.     void Cmd_ProvideRotationToServer (Quaternion objectRotation) {
    34.         syncRot = objectRotation;
    35.      }
    36.  
    37.     [ClientCallback]
    38.     void TransmitRotation() { // Send rotation to server
    39.         if (hasAuthority && Quaternion.Angle(myTransform.rotation, lastRot) > threshold) {
    40.             Cmd_ProvideRotationToServer (myTransform.rotation);
    41.             lastRot = myTransform.rotation;
    42.         }
    43.     }
    44.  
    45. }
    ...but it does not fire at Spawn?

    Anyone that can advise me how to make sure that the rotation is reflected at client at spawn when spawning (host-client) with rotation?
     
  2. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    You could use custom spawn handlers for maximum control over your object spawning behaviors.

    Alternatively (and easy to implement), you could just use the OnStartClient function and set the rotation there (SyncVars are guaranteed to be up-to-date within OnStartClient).

    BTW, why do you need the rotation right away upon spawn? With your above script, it should be set within the next tick of FixedUpdate() anyway.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Thanks, will absolutely try the OnStartClient.

    The user who initiate the match can choose portrait or landscape mode and by that the object must be rotated correctly.
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    @moco2k Thanks for the advice. I did it in the OnStartClient function and got it all working.bI was not aware that the SyncVar do not trigger in OnStartServer.