Search Unity

Resolved Transform doesn't update for other players

Discussion in 'Netcode for GameObjects' started by CommanderPaladin, Jul 5, 2021.

  1. CommanderPaladin

    CommanderPaladin

    Joined:
    Jul 6, 2020
    Posts:
    13
    Hello, I'm new to Networking, and this is actually my first time do Networking. I did follow the Documentation and the examples provided, but, I have a problem.

    I have created a mini-game like "Agar" where you have to collect little green points in order to grow.
    The problem is that, only the server see the changes (when a player eat, it grows).
    The player see himself growing and the server see everyone growing (if they eat) but other players cannot see other players when they grow. But they can feel their collider. Weird.

    I need a little help, to solve this issue, to recap, the players see each other positions, but they don't see the scale of others.

    Here is how the Player prefab looks like:
    upload_2021-7-5_15-52-5.png

    Here is the Multiplayer Object:
    upload_2021-7-5_15-53-12.png

    And here are the Scripts:
    Multiplayer:
    Code (CSharp):
    1. public class Multiplayer : MonoBehaviour
    2. {
    3.  
    4.     // Start is called before the first frame update
    5.     public GameObject food;
    6.     void Start()
    7.     {
    8.         food.SetActive(false);
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.       //
    15.     }
    16.  
    17.     public void Host()
    18.     {
    19.  
    20.         NetworkManager.Singleton.StartHost();
    21.         HideHud();
    22.     }
    23.  
    24.     public void Connect()
    25.     {
    26.  
    27.  
    28.         UNetTransport uNetTransport = this.GetComponent<UNetTransport>();
    29.         uNetTransport.ConnectAddress = GameObject.Find("IpInputText").GetComponent<Text>().text;
    30.         NetworkManager.Singleton.StartClient();
    31.         HideHud();
    32.     }
    33.  
    34.     private void HideHud()
    35.     {
    36.  
    37.         if (NetworkManager.Singleton.IsServer)
    38.         {
    39.             food.SetActive(true);
    40.             NetworkObject[] littleFood = food.GetComponentsInChildren<NetworkObject>();
    41.             for (int i = 0; i < littleFood.Length; i++)
    42.             {
    43.                 littleFood[i].Spawn();
    44.             }
    45.         }
    46.  
    47.         GameObject canvas = GameObject.Find("Canvas");
    48.         canvas.SetActive(false);
    49.     }
    50.    

    Player Script:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         float x = Input.GetAxis("Horizontal") * 0.01f;
    4.         float y = Input.GetAxis("Vertical") * 0.01f;
    5.         if (x != 0 || y != 0)
    6.         {
    7.             transform.position += new Vector3(x, y, 0);
    8.         }
    9.  
    10.         //transform.position = Position.Value;
    11.         //transform.localScale = transform.localScale;
    12.  
    13.     }
    14.     private void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         if (other.gameObject.name.Contains("Food"))
    17.         {
    18.             //Destroy(other.gameObject);
    19.             NetworkManager.Destroy(other.gameObject);
    20.  
    21.  
    22.            transform.localScale += new Vector3(0.2f, 0.2f,0.2f);
    23.  
    24.             //NetworkObject.transform.localScale = transform.localScale;
    25.           //Nothing changes
    26.  
    27.         }
    28.     }
     
  2. CommanderPaladin

    CommanderPaladin

    Joined:
    Jul 6, 2020
    Posts:
    13
    Hello, I've played more with [ServerRpc] and [ClientRpc] and solved the problem. Thanks you.
     
  3. cossacksman

    cossacksman

    Joined:
    Dec 2, 2017
    Posts:
    10
    @CommanderPaladin Would you mind sharing what your solution was? I found myself in this situation and so far I've only partially found a solution...