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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawning clients with the correct rotation

Discussion in 'Multiplayer' started by maikklein, Jul 14, 2015.

  1. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    Code (CSharp):
    1. public class TestNetworkManager : NetworkManager {
    2.  
    3.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    4.     {
    5.         GameObject proxyPlayer = (GameObject)Instantiate(playerPrefab,playerPrefab.transform.position,playerPrefab.transform.rotation);
    6.         NetworkServer.AddPlayerForConnection(conn, proxyPlayer, playerControllerId);
    7.     }
    8. }
    9.  
    I have created a new prefab with a location, rotation. The prefab is spawned correctly on the listenserver but I have some trouble spawning the prefab on the clients.

    If I connect as a client the prefab is spawned in the correct location but the rotation is not applied.

    Have I done anything wrong?
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    You instantiate it with the position and rotation of the prefab. You can change that if you click on your prefab.
     
  3. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    Yes I know and I already did this, but it still spawns differently on the client / server. Maybe this is a bug in 5.1.f1?
     
  4. Bmandk

    Bmandk

    Joined:
    Aug 18, 2012
    Posts:
    70
    Yeah, I had the same problem. What I was make a SyncVar. I instantiate the object. Then set the SyncVar. Then spawn the object to clients. This way, the SyncVar is synced with the spawning. In the start function I just set the rotation to whatever the SyncVar is. Not the most beautiful implementation, but it works.
     
    jjbish likes this.
  5. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    Yeah I did almost the same thing.
     
    jjbish likes this.
  6. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
  7. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    If it is the player object and rotation is important, wouldn't you need a network transform anyway?
     
  8. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    I was suggesting for non-player objects.

    I understand it, but it is another stumbling block that all the samples for spawning follow:

    Instantiate(prefab, pos. rot)
    Spawn()

    Which makes you think it's sending rotation when in fact it isn't. Some of the docs even say it does send rotation. It's only digging deeper into the docs it is apparent its only position when a new or existing client is good to spawn the prefab.
     
  9. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    478
    It's better to have the choice to sync rotation or not to optimise network bandwidth. But I agree the docs can always be improved.
     
  10. schkorpio

    schkorpio

    Joined:
    Apr 15, 2015
    Posts:
    14
    Bit old of course, but the answer is to use a network transform :)
     
  11. jjbish

    jjbish

    Joined:
    Jun 16, 2016
    Posts:
    5
    Thread is obviously old, but thank you Bmandk for offering this suggestion. Was having this same problem and used this method to implement my solution.