A
A
AppingCS2019-04-05 13:40:30
Unity
AppingCS, 2019-04-05 13:40:30

How to make a kill system?

So, I'm working on a simple Shooter and I want to make a kill system, I'll explain how it should work: When one player kills another player, an inscription like UI appears, namely UI TEXT , the line should be something like this: I already tried to do this, y I managed to display the text, and everything worked as I needed, but only in the console, when I tried to display the same text only on the UI, namely by searching by tag, something like this:Killer + "killed" + KilledPlayer

GameObject.FindGameObjectWithTag("MyText").GetComponent<Text>().text = Killer + "kill" + KilledPlayer

But in the end it turned out not what I needed, on the server it seems to show what is needed, and on the client only the nickname of the killed.
You can see more details below.
In my script you can see what I did :
PlayerShooting :
using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
  
 public class PlayerShooting : NetworkBehaviour {
  
         public int Damage = 25;
         public Camera cameraTR;
         private RaycastHit hit;
         private Ray ray;
  
         void Update ()
         {
                 if (Input.GetKeyDown (KeyCode.Mouse0))
                 {
                         Shoot();
                 }
         }
         void Shoot()
         {
                 ray = cameraTR.ScreenPointToRay (Input.mousePosition);
                 if (Physics.Raycast (ray, out hit, 1000))
                 {
                         if(hit.transform.tag == "Player")
                         {
                             
                                 string id = hit.transform.name;
                                 CmdShoot(id, Damage);
                         }
                 }
         }
         [Command]
         void CmdShoot(string Id, int dmg)
         {
                 GameObject go = GameObject.Find (Id);
                 go.GetComponent<PlayerSetup>().Killer = gameObject.name;
                 go.GetComponent<PlayerInfo> ().GetDamage (dmg);
         }
 }

PlayerInfo :
using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
  
 public class PlayerInfo : NetworkBehaviour
 {
         [SyncVar] public int Health = 100;
         // Use this for initialization
         void Start ()
         {
                 transform.name = "Player " + GetComponent<NetworkIdentity> ().netId.ToString ();
         }
         void Update()
         {
 
                 if (Health <= 0)
                 {
                         GetComponent<PlayerSetup>().DisablePlayer();
                 }
         }
         public void GetDamage(int dmg)
         {
                 Health -= dmg;
         }
  
         void OnGUI()
         {
                 if (isLocalPlayer)
                 {
                         GUI.Label(new Rect(Screen.width-100,25,200,50),"Health: "+Health);
                 }
         }
 }

PlayerSetup :
using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
  
 public class PlayerSetup : NetworkBehaviour {
        
         public Camera CharacterCamera;
         public AudioListener CharacterAudioListner;
         public Transform hand1;
         public Transform hand2;
         public Transform clientModel;
         private bool CanResp;
         private float timer;
         public float RespawnTime;
         public string Killer;
  
         void Start()
         {
                 EnablePlayer ();
         }
  
         void EnablePlayer()
         {
                 if (isLocalPlayer)
                 {
                         GetComponent<CharacterController> ().enabled = true;
                         GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
                         CharacterCamera.enabled = true;
                         CharacterAudioListner.enabled = true;
                         hand1.GetComponent<MeshRenderer> ().enabled = true;
                         hand2.GetComponent<MeshRenderer> ().enabled = true;
                         GetComponent<PlayerShooting>().enabled = true;
                 }
                 else
                 {
                         clientModel.gameObject.SetActive(true);
                 }
                 CanResp = false;
                 GetComponent<PlayerInfo> ().Health = 100;
                 transform.position = new Vector3 (0, 1.5f, 0);
         }
         public void DisablePlayer()
         {
                 Debug.Log(Killer +" kill " + gameObject.name);
                 GetComponent<CharacterController> ().enabled = false;
                 GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
                 CharacterCamera.enabled = false;
                 CharacterAudioListner.enabled = false;
                 hand1.GetComponent<MeshRenderer> ().enabled = false;
                 hand2.GetComponent<MeshRenderer> ().enabled = false;
                 GetComponent<PlayerShooting>().enabled = false;
                 clientModel.gameObject.SetActive(false);
                 CanResp = true;
         }
  
         void Update()
         {
                 if (CanResp)
                 {
                         timer+=Time.deltaTime;
                         if(timer>RespawnTime)
                         {
                                 timer = 0;
                                 EnablePlayer();
                         }
                 }
         }
 }

I'm waiting for answers!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2019-04-05
@youkerni

I cannot give you an answer to your question, because I'm not well versed in UNet ..
But I can give you a couple of tips, namely:
1) forget about Find methods in GameObject
2) cache components (especially in Update)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question