Search Unity

UNet Bone and IK synchronization

Discussion in 'UNet' started by jdcart, Mar 17, 2017.

  1. jdcart

    jdcart

    Joined:
    Jun 24, 2015
    Posts:
    15
    Hello,

    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):
    1.  
    2.  
    3. private Transform lookObj;
    4.  
    5. void Update()
    6.     {
    7.         if (lookObj != null)
    8.         {
    9.             return;
    10.         }
    11.  
    12.         lookObj = GameObject.FindGameObjectWithTag("MainCamera").transform;
    13.         Debug.Log(lookObj.name);
    14.     }
    15.  
    16. void OnAnimatorIK()
    17.     {
    18.         if (!anim)
    19.         {
    20.             return;
    21.         }
    22.  
    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");
    31.  
    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.             }
    38.  
    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

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    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

    jdcart

    Joined:
    Jun 24, 2015
    Posts:
    15
    When I have the Debug.Log("lookObj.name"); 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

    jdcart

    Joined:
    Jun 24, 2015
    Posts:
    15
    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

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    188
    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

    jdcart

    Joined:
    Jun 24, 2015
    Posts:
    15
    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!