Home > Back-end >  Setting transform position not working in my vr network (Unity)
Setting transform position not working in my vr network (Unity)

Time:11-08

I am trying to set up a simple multiplayer in VR. For this I have made a scene with a vr controller and a Network Manager, spawning an avatar for each client. I am doing this so the actual VR rig dosnt have to get sent through the network but rather just an Avatar representation. I am setting the avatar to the position of the rig localy and then I only send the position of each avatar bound to the rig to each client. For some reason the avatars spawn for each client but the movement only works for the host. For all other clients the avatars neither move in local space nor on the network. When i run the Debug.Log() that is currently commented out it does print the right position of the target but just dosnt set the transform to that position but rather forces it to (0,0,0). Does anyone know why this could be the case?


FollowTarget Script:

 public class FollowTarget : NetworkBehaviour
 {
     public Transform target;
     public Vector3 offset = new Vector3(0f, 0f, 0f);
     [SerializeField] private bool _isNetworkAvatar;
     [SerializeField] private bool _keepYPosition;
     [SerializeField] private string TagToFollow = "MainCamera";
     private void Start()
     {
         if (_isNetworkAvatar)
         {
             target = GameObject.FindGameObjectWithTag("MainCamera").transform;
             Debug.Log($"Initializing Network Avatar for: {target.gameObject.name}");
         }
     }
     private void Update()
     {
         
         if(!IsOwner) return;
         //Debug.Log($"My Position:{transform.position} should be {target.position}");
         switch (_keepYPosition)
         {
             case true:
                 transform.position = new Vector3(target.position.x, transform.position.y, target.position.z)  
                                      offset;
                 Debug.Log(new Vector3(target.position.x, transform.position.y, target.position.z)  
                           offset);
                 break;
             case false:
                 transform.position = target.position   offset;
                 break;
         }
     }
 }

Avatar Prefab:

enter image description here

CodePudding user response:

Ok, so after many tries I found the solution. The problem was that the client was'nt authorized to change its own position in the network, so i had to add an extra override to the NetworkTransform script which gives the client authorization.

    protected override bool OnIsServerAuthoritative()
    {
        return false;
    }

that fixed it. :)

  • Related