Search Unity

MLAPI score UI player dependent

Discussion in 'Multiplayer' started by nuubje006, Jun 21, 2021.

  1. nuubje006

    nuubje006

    Joined:
    Jan 10, 2018
    Posts:
    2
    I am remaking my old couch multiplayer game into an online multiplayer.
    This is way harder to wrap my head around it. I am looking for something specific but I can't really find anything on google.
    What I want is: 2-8 players can join and fight each other. Each player has a score that increment per kill and all these scores are visible for all players in a UI.

    So I have a ServerRPC that handles all melee attacks. This sits on my player object.
    I can check what clientID attacked someone and if it dies. I tried so use a networkvariable to increment the score in a scoreManager game object that is running on the server, but so far this is not working.
    Now these netvars work for things like health per player, but for score I am confused, because the score UI is not on the player but it should be grouped in 1 canvas on the score manager.
    I don't know how I can keep the score per player and put it in a dynamic UI (2 to 8 textfields)

    I was thinking of making an array that writes the netvar to an index, but this also has to be dynamic and I would love to have some input about this. Any help is greatly appriciated.

    I am new to all this, here is all the code related to this. If there is anything else I need to provide/

    Melee script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnitySampleAssets._2D;
    5. using System.Collections.Generic;
    6. using MLAPI;
    7. using MLAPI.NetworkVariable;
    8. using MLAPI.Messaging;
    9. using System;
    10.  
    11. public class Melee : NetworkBehaviour
    12. {
    13.     public Transform swordClashPrefab;
    14.     public List<GameObject> meleeColliders = new List<GameObject>();
    15.     private PlatformerCharacter2D character; //Character that is melee attacking
    16.     private Animator anim;
    17.     public ScoreManager scoreManager;
    18.  
    19.     public Image cooldownBar;
    20.     public float fillAmount = 1.0f;
    21.  
    22.     //Network Variables
    23.     public NetworkVariableBool net_meleeAttack = new NetworkVariableBool();         // For determining if the player is attacking with melee over the network
    24.     public NetworkVariableVector3 net_colPos = new NetworkVariableVector3();
    25.     public NetworkVariableQuaternion net_colRot = new NetworkVariableQuaternion();
    26.  
    27.     //Variables
    28.     public bool meleeAttack = false;
    29.     float meleeAttDelay;
    30.     public float fastMeleeAttSpd = 0.25f; //Time between 2 attacks in seconds while your attack speed is boosted
    31.     public float normalMeleeAttSpd = 0.5f; //Time between 2 attacks in seconds for normal attacks
    32.     public float lastMeleeAttTimer = -999f;
    33.  
    34.     private void Awake()
    35.     {
    36.         character = gameObject.transform.parent.gameObject.GetComponent<PlatformerCharacter2D>();
    37.         anim = gameObject.transform.parent.gameObject.GetComponent<Animator>(); //so we can do the melee animation
    38.         scoreManager = GameObject.Find("ScoreManager").GetComponent<ScoreManager>();
    39.     }
    40.  
    41.         void OnTriggerStay2D(Collider2D col)
    42.     {
    43.         if (!meleeColliders.Contains(col.gameObject))
    44.             meleeColliders.Add(col.gameObject);
    45.     }
    46.  
    47.     void OnTriggerExit2D(Collider2D col)
    48.     {
    49.         if (meleeColliders.Contains(col.gameObject))
    50.             meleeColliders.Remove(col.gameObject);
    51.     }
    52.  
    53.     public void doMelee(bool melee = false, bool fasterMelee = false)
    54.     {
    55.         //If player is using melee set it to true, but we don't reset the variable here, this we handle in the melee script
    56.         if (meleeAttack == false && IsLocalPlayer)
    57.         {
    58.             if (fasterMelee){meleeAttDelay = fastMeleeAttSpd; } //double attack speed
    59.             else { meleeAttDelay = normalMeleeAttSpd; }            //normal attack rate
    60.  
    61.             if (melee && Time.time > lastMeleeAttTimer + meleeAttDelay) //we add a delay to the melee to balance it
    62.             {
    63.                 lastMeleeAttTimer = Time.time;
    64.  
    65.                 anim.SetBool("Melee Horizontal", true); //Start the animation
    66.                 meleeAttack = true;  
    67.                 UpdateMeleeServerRPC(true);
    68.  
    69.             }
    70.             else
    71.             {
    72.                 anim.SetBool("Melee Horizontal", false);
    73.                 meleeAttack = false;
    74.                 UpdateMeleeServerRPC(false);
    75.             }
    76.         }
    77.     }
    78.  
    79.     private void Update()
    80.     {
    81.         fillAmount = (Time.time- lastMeleeAttTimer) / meleeAttDelay;
    82.         cooldownBar.fillAmount = fillAmount;
    83.     }
    84.  
    85.     void FixedUpdate()
    86.     {
    87.             MeleeHandlerServerRPC();
    88.  
    89.             if (meleeColliders.Count > 0)
    90.             {
    91.                     Vector3 colPos = net_colPos.Value;
    92.                     Quaternion colRot = net_colRot.Value;
    93.  
    94.                     //First test if we are actually melee attacking
    95.                     if (net_meleeAttack.Value || (IsLocalPlayer && meleeAttack))
    96.                     {
    97.                         float direction = character.net_updateScale.Value.x;
    98.  
    99.                         if (swordClashPrefab.gameObject != null)
    100.                         {
    101.                             Transform _swordClashPrefab = Instantiate(swordClashPrefab, gameObject.transform.position + new Vector3(8 * direction, 0, 0), gameObject.transform.rotation) as Transform;
    102.                             Destroy(_swordClashPrefab.gameObject, 1f);
    103.                         }
    104.                     }
    105.                 UpdateMeleeServerRPC(meleeAttack);
    106.             }
    107.             //RESET THE MELEE VARIABLE SO WE CAN CALL IT AGAIN
    108.             meleeAttack = false;
    109.        
    110.     }
    111.  
    112.     [ServerRpc]
    113.     void UpdateMeleeServerRPC(bool netMeleeAttack)
    114.     {
    115.         net_meleeAttack.Value = netMeleeAttack;
    116.     }
    117.  
    118.     [ServerRpc]
    119.     void MeleeHandlerServerRPC()
    120.     {
    121.         if (meleeColliders.Count > 0)
    122.         {
    123.             for (int i = meleeColliders.Count - 1; i >= 0; i--)
    124.             {
    125.                 if (meleeColliders.Count < i) { continue; }
    126.  
    127.                 GameObject col = meleeColliders[i];
    128.  
    129.                 if (col.gameObject == null) { continue; }
    130.  
    131.                 net_colPos.Value = col.transform.position;
    132.                 net_colRot.Value = col.transform.rotation;
    133.  
    134.                 //First test if we are actually melee attacking
    135.                 if (net_meleeAttack.Value)
    136.                 {
    137.                     //Here we will do damage to the hit object / player
    138.                     var player = col.gameObject.GetComponent<PlayerHealth>();
    139.                     if (player != null)
    140.                     {
    141.                         bool killed = player.TakeDamage(50);
    142.                         Debug.Log("Player " + OwnerClientId + " score = + 1");
    143.                         scoreManager.UpdateScore(killed);
    144.                     }
    145.  
    146.                         Debug.Log("Player " + OwnerClientId + " score = " + scoreManager.net_playerScore.Value);
    147.                         //should also add something for objects.
    148.                     }
    149.             }
    150.         }
    151.     }
    152. }
    ScoreManager Script
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnitySampleAssets._2D;
    5. using System.Collections.Generic;
    6. using MLAPI;
    7. using MLAPI.NetworkVariable;
    8. using MLAPI.Messaging;
    9. using System;
    10.  
    11. public class ScoreManager : NetworkBehaviour
    12. {
    13.             //Network Variables
    14.             public NetworkVariableInt net_playerScore = new NetworkVariableInt(0);
    15.  
    16.             public void UpdateScore(bool playerScoreIncrement)
    17.             {
    18.                 UpdateScoreServerRPC(playerScoreIncrement);
    19.             }
    20.  
    21.             [ServerRpc]
    22.              void UpdateScoreServerRPC(bool netplayerScoreIncrement)
    23.             {
    24.                 if (netplayerScoreIncrement)
    25.                 {
    26.                     net_playerScore.Value += 1;
    27.                 }
    28.             }
    29.  
    30. }
    31.