Search Unity

How inform a bullet who is the owner?

Discussion in 'Multiplayer' started by BrunoMikoski, Mar 29, 2016.

  1. BrunoMikoski

    BrunoMikoski

    Joined:
    Feb 25, 2014
    Posts:
    5
    Hello everyone, I'm not a truly a multiplayer programmer, but I've done a few multiplayer games with TNET, and now I'm starting playing around with UNET, but for some reason I can't get it right, in any way, I've followed and read almost everything that I can found online, but is everything mostly the same, add network manager, add identity, assing player, add network transform, BUM! Done.
    But I'm trying to do something really simple like:
    Every player, spawn on sphere when mouse button down, at some random position with one specific color, if is the first player use blue, if is the second use red. Just like that.

    I just can't get this to work properly, from my ~experience~, what I should do is, as soon one player spawn, I register that to the server, and inform every client about the color this player should use, and every time I spawn a new sphere, I inform all the clients that this player owns that ball, so they should have the color as the player.

    If someone can light me up with this, will be awesome.

    This is basically what I'm trying to do:


    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5.  
    6.  
    7. public struct PlayerColorReference
    8. {
    9.     public uint player;
    10.     public Color color;
    11. }
    12.  
    13. public class PlayersColors : SyncListStruct<PlayerColorReference>
    14. {
    15. }
    16. public class GameplayController : NetworkBehaviour
    17. {
    18.     private Color[] sphereColors = new Color[] {Color.blue, Color.red, Color.green, Color.cyan};
    19.  
    20.     private PlayersColors playersColors = new PlayersColors();
    21.  
    22.  
    23.  
    24.     [Command]
    25.     public void CmdRegisterWithServer(GameObject o)
    26.     {
    27.         PlayerScript playerScript = o.GetComponent<PlayerScript>();
    28.  
    29.         PlayerColorReference playerColorReference = GetOrCreatePlayerColorReference(playerScript.NetworkIdentity.netId.Value);
    30.  
    31.         playerScript.RpcAssignColor(playerColorReference.color);
    32.     }
    33.  
    34.     private PlayerColorReference GetOrCreatePlayerColorReference(uint value)
    35.     {
    36.         foreach (PlayerColorReference playerColorReference in playersColors)
    37.         {
    38.             if (playerColorReference.player == value)
    39.                 return playerColorReference;
    40.         }
    41.         PlayerColorReference newPlayerColorReference = new PlayerColorReference
    42.         {
    43.             player = value, color = sphereColors[(playersColors.Count + 1)%sphereColors.Length]
    44.         };
    45.  
    46.         playersColors.Add(newPlayerColorReference);
    47.  
    48.         return newPlayerColorReference;
    49.     }
    50. }
    51.  

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerScript : NetworkBehaviour
    5. {
    6.     [SerializeField]
    7.     private GameObject targetSphere;
    8.     [SyncVar]
    9.     private Color sphereColor;
    10.  
    11.     private NetworkIdentity networkIdentity;
    12.     private GameplayController gameplay;
    13.     public NetworkIdentity NetworkIdentity
    14.     {
    15.         get { return networkIdentity; }
    16.     }
    17.  
    18.     private void Awake()
    19.     {
    20.         gameplay = FindObjectOfType<GameplayController>();
    21.         networkIdentity = GetComponent<NetworkIdentity>();
    22.     }
    23.  
    24.     public override void OnStartAuthority()
    25.     {
    26.         base.OnStartAuthority();
    27.         gameplay.CmdRegisterWithServer(this.gameObject);
    28.  
    29.     }
    30.  
    31.     void Update ()
    32.     {
    33.         if(!isLocalPlayer)
    34.             return;
    35.  
    36.         if (Input.GetMouseButtonDown(0))
    37.         {
    38.             CmdSpawnSphere();
    39.         }
    40.     }
    41.  
    42.  
    43.     [Command]
    44.     private void CmdSpawnSphere()
    45.     {
    46.         GameObject newSphere = Instantiate(targetSphere);
    47.         Sphere sphere = newSphere.GetComponent<Sphere>();
    48.         targetSphere.transform.position = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
    49.         NetworkServer.Spawn(newSphere);
    50.         sphere.RpcAssignColor(sphereColor);
    51.  
    52.     }
    53.  
    54.     [ClientRpc]
    55.     public void RpcAssignColor(Color color)
    56.     {
    57.         sphereColor = color;
    58.     }
    59. }
    60.  

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class Sphere : NetworkBehaviour
    5. {
    6.     private Material material;
    7.     private PlayerScript owner;
    8.  
    9.     private void Awake()
    10.     {
    11.         material = GetComponent<Renderer>().material;
    12.     }
    13.  
    14.  
    15.     [ClientRpc]
    16.     public void RpcAssignColor(Color targetColor)
    17.     {
    18.         material.color = targetColor;
    19.     }
    20. }
    21.  
    /SPOILER]
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I may be missing something. First, I'm assuming the sphere is the bullet? What does having a sphere spawn at a random position with a color have to do with "who is the owner"? Are you wanting explicit control over the sphere/bullet by the player? Or are you simply wanting to instantiate the sphere/bullet with a color that matches the spawning player?
     
  3. badawe

    badawe

    Joined:
    Jan 17, 2011
    Posts:
    297
    Sorry the title is a bit confusing, but you described exactly right, I'm just working in a simpler environment to understand it properly, is simple spawning one sphere on all clients with the proper color on it. and the color is from the server, one for each player.
     
  4. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    In CmdSpawnSphere(), you can send the players' NetworkInstanceId as a parameter to let the server know who spawned it. An object's NetworkInstanceId is synced across all clients, and can be used to differentiate players and other objects. Like this:

    CmdSpawnSphere(NetworkInstanceId playerWhoSpawnedSphere);

    Then when you want to call it, just do: CmdSpawnSphere(netId);

    You'd probably have to keep a list of which NetworkInstanceId is which player, that way you can keep track of what color to make each sphere.