- Home /
Kill System, who killed who (UNET)
I already make kill system , and now console show who kill who , now I need make it in UI TEXT Here this code :
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);
}
}
And here code 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);
}
}
}
And 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();
}
}
}
}
Now I need to display this text on UI TEXT so that all clients including the server can see it.
I showed everything , I hope what I get help :)
Answer by Magso · Apr 02, 2019 at 03:45 PM
Make a boolean that turns true in the PlayerShooting script
if(hit.transform.tag == "Player")
{
hit.transform.gameObject.GetComponent<PlayerSetup>().Killer = gameObject.name;
isTheKiller = true;
//...
and check for it in the PlayerInfo script
if (Health <= 0 && GetComponent<PlayerShooting>().isTheKiller == true)
{
GetComponent<PlayerShooting>().isTheKiller = false;
GetComponent<PlayerSetup>().DisablePlayer();
}
O$$anonymous$$, I already did it, now the problem is different, the message is output to the console as it should be both from the client’s side and from the server side, now I want to output this text to the UI text, now I’m trying to do it but to no avail . I wait for your answer !
I updated my script above, now you can see how I did it, now the biggest problem is to display this text on the UI TEXT
add
using UnityEngine.UI;
$$anonymous$$ake a 'text' variable
public Text theText;
and change the text like this
theText.text = $$anonymous$$iller +" kill " + gameObject.name;
Okay , but when network manager instantiate player , that player doesn't to find theText , maybe me should use GameObject.Find("NameText").GetComponent<Text>().text = $$anonymous$$iller +" kill " + gameObject.name;
, this text will be show in client and in server ? I think me should use [Command] and [RpcClient] .