Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Anyone knows how to spawn a prefab in the correct position?

Discussion in 'Multiplayer' started by asperatology, Jul 22, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I wanted to have my player unit to spawn on top of the red marker or the blue marker, depending on whether or not the player is the host or client.



    I looked online for UNET tutorials, and one of the videos I saw mentioned how a prefab needs to have NetworkTransform component added in order to sync the game object's Transform across the network.

    I don't know if NetworkTransform has anything to do with how Network Manager handles "Auto Create Player". But I guessed it can be used to set the initial spawn position when the Network Manager is auto-creating the player prefab.

    These are my questions:
    • How would you modify it so that it doesn't spawn at the position, (0, 0, 0), and spawn in the position I want to specify at?
    • I have to make a script for this, correct? If yes, could someone point me to the right direction?
    • All of these have nothing to do with NetworkTransform, correct? Other than syncing the game object's Transform, there's nothing else that can be done with them?
     
  2. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    You don't need a NetworkTransform for spawning. Every spawn object has a NetworkIdentity and on spawn it will send the game objects transform position (not rotation and scale though).

    However a caveat on that, when new players join and receive the existing players position to spawn them they will appear at whatever position the server currently has them at.

    Auto create simply just spawns the player and IF you have any NetworkSpawn objects in the scene it can randomly or round robin set their position to one of those.

    Without a NetworkTransform that position will not continuously update.
     
  3. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Well to create spawn points just make empty game objects at those positions, and add NetworkStartPosition component (not sure if thats the name, im on my phone) to them. The manager will use those as spawn points. And set the spawn method to round robin. That way itll always spawn your host at the first point, and second player on second. And third on first again, but if i understood right you only have two players.
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I did not know that. I thought NetworkStartPosition is for something else, but I never understood it until now. Seems more logical.


    Correct. It's only for 2 players. I will only scale up the game to more players if and only if the gameplay is decent enough to warrant this.
     
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    This is an old thread. I've already learned about spawning the prefabs in the correct positions. I feel it is worth putting the following quote in this thread for completeness.

    Your NetworkManager is set to choose spawn points randomly. In the NetworkManager component, there's a dropdown menu where you can select Round Robin or Random for Spawn Locations. Choose "Round Robin" so the players always spawned at different points in descending order.

    If you would like to set red players to red spawn, blue players to blue spawn, and both teams do not swap spawn locations, it would be best to hardcode the spawning by setting their positions the moment the units are spawned into the game.
     
  6. BenHowick

    BenHowick

    Joined:
    Jan 11, 2018
    Posts:
    10
    You say this but may I please ask what you mean by this. Do you mean to have a single spawn point or something like this and then when they spawn teleport them to the correct position? I need a simple way of spawning red players on the red side and blue players on the blue side. Any help would be appreciated.