x


Different Player Colours (Multiplayer)

Hello! I try to make a simple Multiplayer FPS Game with up to 6 Players. I have a simple spawner but I want to have a spawner which spawns players with different colours.(So 6 different colours) Here is my spawner

using UnityEngine;

using gui = UnityEngine.GUILayout;



public class GameMenu : MonoBehaviour

{

    public GameObject PlayerPrefab;

    string ip = "127.0.0.1";



    public void Awake()

    {

       Application.runInBackground = true;

    }



    public void CreatePlayer()

    {

        connected = true;

        var g = (GameObject)Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 1);

       g.networkView.stateSynchronization = NetworkStateSynchronization.Unreliable;

        g.camera.enabled = true;

        camera.enabled = false;

    }

    void OnDisconnectedFromServer()

    {

        connected = false;

    }

    void OnPlayerDisconnected(NetworkPlayer pl)

    {

        Network.DestroyPlayerObjects(pl);

    }

    void OnConnectedToServer()

    {

        CreatePlayer();

    }

    void OnServerInitialized()

    {

        CreatePlayer();

    }

    bool connected;

    void OnGUI()

    {

        if (!connected)

        {

            ip = gui.TextField(ip);

            if (gui.Button("connect"))

            {

                Network.Connect(ip, 5300);

            }

            if (gui.Button("start"))

            {

                Network.InitializeServer(10, 5300, false);

            }

        }

    }

}

Should I add 6 different player prefabs to the project? If yes, how do I let them spawn in a row, if the first player connects the (blue) player 1 spawns? Can somebody help me out?

more ▼

asked Jun 02 '12 at 01:54 PM

potu1304 gravatar image

potu1304
18 3 7 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You could probably use Network.connections.length with the 6 prefabs.

It would be something like this I think:

public GameObject FirstPrefab;
public GameObject SecondPrefab;
...

void OnConnectedtoServer(){
switch(Network.connections.length){
    case(1)
      CreatePlayer(SecondPrefab);//you would have chance the function to CreatePlayer(GameObject PlPref)...
    case(2)
      CreatePlayer(ThirdPrefab);
    ....
}

void OnServerInitialized()
{
CreatePlayer(FirstPrefab);
}

Maybe you don't even need to make 6 prefab. Maybe you can change between different materials/textures.

I hope my answer gives you an idea.

more ▼

answered Jun 02 '12 at 02:28 PM

ExTheSea gravatar image

ExTheSea
2k 12 23 30

Yes thats an good idea, but isn't i a fault to have more than one CreatePlayer?

Jun 02 '12 at 06:25 PM potu1304

what do you mean? CreatePlayer(...) gets only called ones per player who connects to the server. Do you know how switch-case works?

Jun 02 '12 at 06:47 PM ExTheSea

Oh and what do you mean with PlPref?

Jun 02 '12 at 06:52 PM potu1304

I mean need to change your CreatePlayer to something like:

CreatePlayer(GameObject PlPref)

PlPref is just a variable in this case so you can access the different prefabs when you instantiate the player. Do you understand what i want to say?

Jun 02 '12 at 07:42 PM ExTheSea
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3340
x711
x683
x130
x27

asked: Jun 02 '12 at 01:54 PM

Seen: 501 times

Last Updated: Jun 02 '12 at 07:42 PM