Search Unity

UNET Network Transform Child not synchronizing

Discussion in 'UNet' started by joestyler, Apr 8, 2019.

  1. joestyler

    joestyler

    Joined:
    Apr 4, 2019
    Posts:
    7
    Hi guys,

    following issue:

    I have a pretty basic unet multiplayer game, with multiple players running around... all working so far. but now i want each player's legs to face the direction which he's walking. i'm doing this by using transform.localEulerAngles and it works well in for each player (no matter if host or client), but one player doesn't see other player's legs rotating, so there is no synchronization.

    the legs are a child object of the player, who has a network identity and network transform and a network transform child component attached (with the legs attached as the child target).

    The player has a PlayerMovement script, where the walkingRight, walkingLeft, walkingForward etc. variables of the LegsRotation script are adjusted to the current movement. the LegsRotation script should then handle the rest of it:

    PlayerMovement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerMovement : NetworkBehaviour
    7. {
    8.     //Movement Variables
    9.     public float speed;      
    10.     public float sensitivity = 3f;
    11.     private float yaw = 0f;
    12.  
    13.     public GameObject legs;
    14.     public Animator anim;
    15.     public Transform tf;
    16.  
    17.     Rigidbody rb;
    18.    
    19.     private void Start()
    20.     {
    21.         tf = GetComponent<Transform>();
    22.         rb = GetComponent<Rigidbody>();
    23.         anim = GetComponent<Animator>();                      
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.        [COLOR=#000000] //Only for local player...
    29.         if (!isLocalPlayer)
    30.         {
    31.             return;
    32.         }
    33.  
    34.         //Movement    
    35.         legs.GetComponent<LegsRotation>().walkingRight = false;
    36.         legs.GetComponent<LegsRotation>().walkingLeft = false;
    37.         legs.GetComponent<LegsRotation>().walkingForwards = false;
    38.         legs.GetComponent<LegsRotation>().walkingBackwards = false;
    39.         if (Input.GetKey(KeyCode.W))
    40.         {
    41.             walk(0, 0, 1);
    42.             legs.GetComponent<LegsRotation>().walkingForwards = true;
    43.         }
    44.         if (Input.GetKey(KeyCode.S))
    45.         {
    46.             walk(0, 0, -1);
    47.             legs.GetComponent<LegsRotation>().walkingBackwards = true;
    48.         }
    49.         if (Input.GetKey(KeyCode.D))
    50.         {
    51.             walk(1, 0, 0);
    52.             legs.GetComponent<LegsRotation>().walkingRight = true;
    53.         }
    54.         if (Input.GetKey(KeyCode.A))
    55.         {
    56.             walk(-1, 0, 0);
    57.             legs.GetComponent<LegsRotation>().walkingLeft = true;
    58.         }[/COLOR]
    59.  
    60.         //Mouse View                                                  
    61.         yaw += sensitivity * Input.GetAxis("Mouse X");
    62.         transform.eulerAngles = new Vector3(0f, yaw, 0f);
    63.     }
    64.  
    65.     void walk(int x, int y, int z)
    66.     {
    67.         tf.Translate(x*speed, y*speed, z*speed);
    68.     }
    69. }

    LegsRotation script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LegsRotation : MonoBehaviour
    6. {
    7.     public bool walkingRight = false;
    8.     public bool walkingLeft = false;
    9.     public bool walkingForwards = false;
    10.     public bool walkingBackwards = false;
    11.  
    12.     private void Update()
    13.     {
    14.         transform.localEulerAngles = new Vector3(270f, 0f, 0f);
    15.  
    16.         if (walkingForwards && !walkingBackwards)
    17.         {
    18.             transform.localEulerAngles = new Vector3(270f, 0f, 0f);
    19.             if (walkingRight && !walkingLeft) transform.localEulerAngles = new Vector3(270f, 45f, 0f);
    20.             if (walkingLeft && !walkingRight) transform.localEulerAngles = new Vector3(270f, 315f, 0f);
    21.         }
    22.         if (walkingBackwards && !walkingForwards)
    23.         {
    24.             transform.localEulerAngles = new Vector3(270f, 180f, 0f);
    25.             if (walkingRight && !walkingLeft) transform.localEulerAngles = new Vector3(270f, 135f, 0f);
    26.             if (walkingLeft && !walkingRight) transform.localEulerAngles = new Vector3(270f, 225f, 0f);
    27.         }
    28.         if (walkingRight && !walkingForwards && !walkingBackwards && !walkingLeft)
    29.         {
    30.             transform.localEulerAngles = new Vector3(270f, 90f, 0f);
    31.         }
    32.         if (walkingLeft && !walkingForwards && !walkingBackwards && !walkingRight)
    33.         {
    34.             transform.localEulerAngles = new Vector3(270f, 270f, 0f);
    35.         }
    36.     }
    37. }
    1.png 2.png

    do you have any idea, why it isn't working? i thought that every transformation/rotation gets synchronized, when you do network transform, no matter how the transformation is initiated... do i have to run the code for this on the server?

    Thanks for your ideas and also for studying my case :D
     
  2. joestyler

    joestyler

    Joined:
    Apr 4, 2019
    Posts:
    7
    Ok problem solved ..
     
  3. Deleted User

    Deleted User

    Guest

    It would be nice if you tell us how you solved it!
     
    Sludgemonster likes this.
  4. Chuck_S

    Chuck_S

    Joined:
    Dec 11, 2018
    Posts:
    15
    Omg what was the solution. I have the same problem :(
     
  5. Deleted User

    Deleted User

    Guest

    It seems joestyler sadly make a mystery about it..
     
  6. kalibcrone

    kalibcrone

    Joined:
    Sep 4, 2015
    Posts:
    10
    Would you like to share your answer? Other people could benefit from knowing your answer
     
  7. kalibcrone

    kalibcrone

    Joined:
    Sep 4, 2015
    Posts:
    10
    Okay, I had a very similar issue. My players would move around in the world, but their heads would not rotate for each other except locally.

    Not sure if my issue is the same as you, but I was accidentally only grabbing the transform of my player's head model, but technically it was the head's parent gameobject that was moving the model, so both the gameobject with the mesh renderer for the 3d model as well as the parent gameobject which rotated the model, needed to be dragged into a NetworkTransformChild component on the root gameobject.

    In my case it was just a silly mistake, but had the same result, so I figured I'd at least throw it as a possible answer.
     
  8. DEV_3EGaming

    DEV_3EGaming

    Joined:
    Apr 3, 2021
    Posts:
    1
    For anyone still looking for this and using mirror i found that enabling Client Authority makes it work. upload_2021-6-25_15-10-27.png
     
    Alexx019 likes this.