Search Unity

UNET Network Transform Child not synchronizing

Discussion in 'Connected Games' 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. embrain

    embrain

    Joined:
    Mar 12, 2019
    Posts:
    4
    It would be nice if you tell us how you solved it!
     
  4. Chuck_S

    Chuck_S

    Joined:
    Dec 11, 2018
    Posts:
    1
    Omg what was the solution. I have the same problem :(
     
  5. embrain

    embrain

    Joined:
    Mar 12, 2019
    Posts:
    4
    It seems joestyler sadly make a mystery about it..