Answer the question
In order to leave comments, you need to log in
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
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);
}
}
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);
}
}
}
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();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question