Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

UNet Bone and IK synchronization

Discussion in 'Multiplayer Networking' started by jdcart, Mar 17, 2017.

  1. jdcart


    Jun 24, 2015

    I have a non-player character that gets spawned. I have a couple of animations that play properly and I just added some code for aiming at players, the code rotates the head and spine of the npc to aim directly at the first person camera of the player.

    Code (CSharp):
    3. private Transform lookObj;
    5. void Update()
    6.     {
    7.         if (lookObj != null)
    8.         {
    9.             return;
    10.         }
    12.         lookObj = GameObject.FindGameObjectWithTag("MainCamera").transform;
    13.         Debug.Log(;
    14.     }
    16. void OnAnimatorIK()
    17.     {
    18.         if (!anim)
    19.         {
    20.             return;
    21.         }
    23.         if (ikActive)
    24.         {
    25.             //lookObj is the Player camera
    26.             if (lookObj != null)
    27.             {
    28.                 anim.animator.SetLookAtWeight(1);
    29.                 anim.animator.SetLookAtPosition(lookObj.transform.position);
    30.                 Debug.Log("Head rotation set");
    32.                 spineTransform = anim.animator.GetBoneTransform(HumanBodyBones.Spine);
    33.                 newRotation = Quaternion.LookRotation(lookObj.transform.position - spineTransform.position, Vector3.up);
    34.                 newRotation.y = newRotation.y - 0.2f;
    35.                 anim.animator.SetBoneLocalRotation(HumanBodyBones.Spine, newRotation);
    36.                 Debug.Log("Body rotation set");
    37.             }
    39.             else
    40.             {
    41.                 anim.animator.SetLookAtWeight(0);
    42.             }
    43.         }
    44.     }
    Currently, as a new client joins, the camera is referenced correctly and the npc aims directly at the camera, however, the movement is only visible on the server side. The npc on the client side never gets the movement updates.

    I have a couple of theories for this. The first one, is that I'm referencing the camera instance on the server side, but not the camera instance on the client side, so the npc never gets updated. The second one is that IK and transforms that are made directly on the skeleton need to be send individually to each of the clients-- I don't know how to go about this.

    If any one has an idea of how to solve this, I appreciate the help!
  2. angusmf


    Jan 19, 2015
    It looks like what you have should run on both sides. Are you getting error messages about null references on the client? Are any of those log messages getting written on the client? The debugger should help you track down what's different.
    jdcart likes this.
  3. jdcart


    Jun 24, 2015
    When I have the Debug.Log(""); I get a "BaseObject: NotAvailableDuringDebugging" error in Visual Studio, but I don't think that is related to the synchronization. Other than that, no errors at all.

    I tried to run a standalone version of the app as a server and connect with the editor as a client to be able to see the errors directly in the editor, but the editor never connected, so I just moved on.

    Again, I think there is a problem with the client not referencing the camera properly, which is weird because the player is the one that spawns the camera in the first place, and until it spawns the server won't find a reference. As the client joins, the server gets the reference right away but not the client.
  4. jdcart


    Jun 24, 2015
    I built a clean scene with just my player and the npc to test this specific code and functionality and the synchronization worked properly. I'm not sure yet what I'm missing on my actual scene, maybe some of the other code that I have is the problem.
  5. voncarp


    Jan 3, 2012
    That code seems to be client side only. The lookObj needs to be synced properly on server and client for you to get your desired effect.

    Often, camera functionality is client side only, particularly if you are aiming weapons. So, if you are aiming a weapon. You want to sync a different object that is controlled by the client of the network. For instance, sync a target object that the camera is pointing at over the network.
  6. jdcart


    Jun 24, 2015
    Hi, thanks for chiming in. Actually this code works fine on server and client. In my case the camera is already being synced properly because it is part of the player prefab (a first person player), also, it's position is synchronized through the "Network Transform" component. Having said that, I found what my problem was, and it was actually very simple. My start method, where I get the reference to the Network Animator, had a [Server Callback] so it was only running on the server. Very silly mistake on my part, thanks everyone for the help!