Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotation isnt synced

Discussion in 'Netcode for GameObjects' started by SkippyProduction, Jun 7, 2023.

  1. SkippyProduction

    SkippyProduction

    Joined:
    Dec 2, 2022
    Posts:
    1
    I cant seem to find anything on spawning an object when a player joins and Ive been stuck on this for weeks
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using Unity.Netcode;
    4.  
    5. public class PlayerManager : NetworkBehaviour
    6. {
    7.     private readonly NetworkVariable<int> _rotation = new NetworkVariable<int>(0);
    8.     [SerializeField] private GameObject playerBoardPrefab;
    9.  
    10.     private void Update()
    11.     {
    12.         Debug.Log(OwnerClientId + "; clientInt:" + _rotation.Value);
    13.     }
    14.  
    15.     public override void OnNetworkSpawn()
    16.     {
    17.         if (!IsServer) return;
    18.         NetworkManager.Singleton.OnClientConnectedCallback += SpawnPlayer;
    19.     }
    20.  
    21.     public override void OnDestroy()
    22.     {
    23.         if (IsServer)
    24.         {
    25.             NetworkManager.Singleton.OnClientConnectedCallback -= SpawnPlayer;
    26.         }
    27.     }
    28.  
    29.     private void SpawnPlayer(ulong clientId)
    30.     {
    31.         int rotation = _rotation.Value;
    32.         GameObject playerBoard = Instantiate(playerBoardPrefab);
    33.         playerBoard.transform.GetChild(0).GetComponent<RectTransform>().rotation = Quaternion.Euler(0, 0, rotation);
    34.         NetworkObject networkObject = playerBoard.GetComponent<NetworkObject>();
    35.         networkObject.Spawn();
    36.  
    37.         _rotation.Value = rotation+90;
    38.     }
    39. }
    what am I missing here
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    145
    Hi @SkippyProduction,
    Actually this code isn't enough to understand of your structure.
    I really high recommend you "code monkey" tutorial video
    There are 2 ways of creating PlayerPrefab object. One is automatically and other is manually.
    Create automatically is simple and easy and that video will show you this way.

    PlayerManager object is scene object or you create manually?
     
    RikuTheFuffs likes this.