Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Discussion How to make multiplayer knockdown system in unity ?

Discussion in 'Scripting' started by teanteagame, Oct 31, 2022.

  1. teanteagame

    teanteagame

    Joined:
    Jul 15, 2019
    Posts:
    5
    I am using Photon PUN to make a multiplayer game.
    Right now I'm making the knockdown system like the battle royale's team mode. I want when the whole team is down, the game will be over. Does anyone have a solution to check when all players have been knocked?
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    636
    I mean, where to even start here. I suppose the simplest answer would be ... keep track of these knocked down states per player, and when all players are knocked down, well, thats a 'true'.

    But, honestly, i think this is just too vauge and missing to much all around to be at a stage where this boolean is even useful. Im guessing that atm, this system doesnt even exist, merely because, in fact, to get a state of 'knocked down' is rather easy, if you have all the framework in place.

    Do you have any example of what you have, and where you are in this procedure so far?
     
  3. teanteagame

    teanteagame

    Joined:
    Jul 15, 2019
    Posts:
    5
    I have found a solution. I code like this:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DummyManager : MonoBehaviour
    6. {
    7.     public Transform spawnDummyPoint;
    8.     public DummyTest[] dummies;
    9.  
    10.     public DummyTest dummy;
    11.  
    12.     [SerializeField]bool isAllKnockedDown;
    13.  
    14.     void Update()
    15.     {
    16.         dummies = FindObjectsOfType<DummyTest>();
    17.         isAllKnockedDown = isAllTrue();
    18.  
    19.         if(Input.GetKeyDown(KeyCode.K))
    20.         {
    21.             Instantiate(dummy, spawnDummyPoint.transform.position, Quaternion.identity);
    22.         }
    23.  
    24.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.         RaycastHit hit;
    26.  
    27.         if(Physics.Raycast(ray, out hit))
    28.         {
    29.             if(hit.transform.GetComponent<DummyTest>())
    30.             {
    31.                 hit.transform.GetComponent<DummyTest>().isKnockDown = true;
    32.             }
    33.         }
    34.     }
    35.  
    36.     bool isAllTrue()
    37.     {
    38.         for(int i = 0; i < dummies.Length; i++)
    39.         {
    40.             if(dummies[i].isKnockDown == false)
    41.             {
    42.                 return false;
    43.             }
    44.         }
    45.  
    46.         return true;
    47.     }
    48. }
    49.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DummyTest : MonoBehaviour
    6. {
    7.     public bool isKnockDown;  
    8. }
    9.  
    First create a script to manage all players, then create a script that assigns to a player to store his data. In Dummy Manager, I have created a bool is isAllTrue() , it will check for players in a loop if any player has not been knocked it will be false

    However, I don't know if code like this is safe if I apply the method with Photon Pun.