Search Unity

[Solved] How to manually spawn a game object onto Network Start Position via script?

Discussion in 'Multiplayer' started by asperatology, Dec 3, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I have an AI game object (that is technically the host's game object) that I want to spawn exactly at the position where a Network Start Position is located. When spawning the AI game object, I want to spawn them via Round Robin style.

    I would like to know how to "manually" spawn a game object exactly on the Network Start Position. How would I do this?

    I know that the Network Start Position is used for spawning player game object prefabs via NetworkServer's Spawn Info. Maybe I could use NetworkServer.Spawn() to spawn the game object that I wanted to have on the Network Start Position, but I don't know how I need to achieve this.

    I welcome all ideas. Thanks.

    EDIT:

    I also know that you can use

    Code (csharp):
    1. GameObject[] startPositions = GameObject.FindObjectsWithTag("StartLocation"); //"StartLocation" is the NetworkStartPosition game object's custom tag name that is set in the editor. It's just an example.
    To find all the Network Start Positions, but how would you tell there's an object already at this location? Or that all but 1 Network Start Positions are registered so the other players' units are spawned from their respective locations? (The remaining Network Start Position is only available to the AI player when all players are randomly assigned to a Network Start Position, even when using Round Robin mode.)

    EDIT 2:

    Can you obtain NetworkStartPositions from the NetworkManager? Or from a subclass of NetworkManager?

    EDIT 3:

    I think I got it. Use

    Code (csharp):
    1. NetworkManager.GetStartPosition();
    to obtain a Transform from an available NetworkStartPosition, and just set the game object's position to that obtained Transform's position.
     
    Last edited: Dec 3, 2015
    bittarello likes this.
  2. mohitsabhi

    mohitsabhi

    Joined:
    Feb 11, 2017
    Posts:
    5
    Hello, can you please assist sharing the script for this. Am stuck with this and not getting any solution for a month now. Have two spawn positions and have set the spawn method to round robin, but if the client joins a second time, i.e. by clicking the Stop and join as client on the HUD, the spawn takes place on top of the existing Local player. How do I go around to stop this and assign the start positions check.

    Your help will be highly appreciated.

    Brgds
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Here's my open-source implementation of a completed multiplayer UNET-only real-time strategy game, Multiplier. The use of NetworkManager.GetStartPosition() is used in the project. It may help you with your issue.