Moving a bone on a humanoid character - best way to do this in PUN?

What is the correct procedure for rotating a bone so that the effect on the character mesh appears for all clients?

I've implemented a simple head rotation for my humanoid character where, when you look up with the mouse your head moves up to follow the camera. I have done this in LateUpdate to avoid the rotation being overwritten by the Animator-driven animations.

However, it's not clear how I can transfer this motion to the other client-player models.

There are some similar questions in the forum but I'm wondering if rotating the bone in LateUpdate is already the wrong approach for multiplayer?

Thanks in advance for any pointers you can give me.

Comments

  • Ok. I got this working:

    RotateHeadBone() calculates what the angle of the head should be in relation to the FPS mouselook angle and passes the rotation as a Quaternion to the variable qHead.

    In the OnPhotonSerializeView method I stream the qHead variable over the network.

    Lastly, in the LateUpdate method, if the playerprefab is a remote prefab I apply the rotation sent via OnPhotonSerializeView otherwise the mouse position sets the angle of the character head for the local player.
    private void LateUpdate()
            {
                if (!PV.IsMine) {
                    head.transform.localRotation = qHead;
                    return;
                }
                
                RotateHeadBone();
            }
    
       
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
            {
                if (stream.IsWriting)
                {
                   //headbone
                   stream.SendNext(headAngle);
                   stream.SendNext(qHead);
                }
                else
                {
                   //headbone
                   this.headAngle = (float)stream.ReceiveNext();
                   this.qHead = (Quaternion)stream.ReceiveNext();
                }
            }
    
    public void RotateHeadBone ()
            {
                headAngle = NumberReRange(verticalLookRotation, -89.0f, 89.0f, 10.0f, -30.0f);
                qHead = Quaternion.Euler(headAngle, head.transform.localRotation.y, head.transform.localRotation.z);
                head.transform.localRotation = qHead;
            }
    
    
  • TobiasTobias admin
    Thanks for the update and sharing the idea / code. Always appreciated.
Sign In or Register to comment.