- Home /
Question by
NunoMSSilva19 · May 28, 2020 at 05:34 PM ·
scripting problemfps1st person
Kill, Double Kill, Triple Kill! How I put this in the game?
Hi people, I made a script to put a kill, Double Kill, Triple Kill (voice) in the game. However Doesn´t work... Someone can help me, writing a right script for me! Below is the script I made. Thanks!
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
[RequireComponent(typeof(PlayerSetup))]
public class Player : NetworkBehaviour {
[SyncVar]
private bool _isDead = false;
public bool isDead
{
get { return _isDead; }
protected set { _isDead = value; }
}
[SerializeField]
private int maxHealth = 100;
[SyncVar]
private int currentHealth;
public float GetHealthPct ()
{
return (float)currentHealth / maxHealth;
}
[SyncVar]
public string username = "Loading...";
public int kills;
public int deaths;
[SerializeField]
private Behaviour[] disableOnDeath;
private bool[] wasEnabled;
[SerializeField]
private GameObject[] disableGameObjectsOnDeath;
[SerializeField]
private GameObject deathEffect;
[SerializeField]
private GameObject spawnEffect;
private bool firstSetup = true;
public AudioSource audioPlayer;
public AudioClip[] killStreakSounds;
int currentStreak = 0;
float killTimer = 0;
const float streakReset = 1;
public void SetupPlayer ()
{
if (isLocalPlayer)
{
//Switch cameras
GameManager.instance.SetSceneCameraActive(false);
GetComponent<PlayerSetup>().playerUIInstance.SetActive(true);
}
CmdBroadCastNewPlayerSetup();
}
[Command]
private void CmdBroadCastNewPlayerSetup ()
{
RpcSetupPlayerOnAllClients();
}
[ClientRpc]
private void RpcSetupPlayerOnAllClients ()
{
if (firstSetup)
{
wasEnabled = new bool[disableOnDeath.Length];
for (int i = 0; i < wasEnabled.Length; i++)
{
wasEnabled[i] = disableOnDeath[i].enabled;
}
firstSetup = false;
}
SetDefaults();
}
//void Update()
//{
// if(KillTimer > 0)
// {
// KillTimer -= Time.deltaTime;
// if(KillTimer <= 0) ResetStreak();
// }
// if (!isLocalPlayer)
// return;
// if (Input.GetKeyDown(KeyCode.K))
// {
// RpcTakeDamage(99999);
// }
//}
[ClientRpc]
public void RpcTakeDamage (int _amount, string _sourceID)
{
if (isDead)
return;
currentHealth -= _amount;
Debug.Log(transform.name + " now has " + currentHealth + " health.");
if (currentHealth <= 0)
{
Die(_sourceID);
}
}
private void Die(string _sourceID)
{
isDead = true;
Player sourcePlayer = GameManager.GetPlayer(_sourceID);
if (sourcePlayer != null)
{
sourcePlayer.kills++;
GameManager.instance.onPlayerKilledCallback.Invoke(username, sourcePlayer.username);
}
deaths++;
//Disable components
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = false;
}
//Disable GameObjects
for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
{
disableGameObjectsOnDeath[i].SetActive(false);
}
//Disable the collider
Collider _col = GetComponent<Collider>();
if (_col != null)
_col.enabled = false;
//Spawn a death effect
GameObject _gfxIns = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(_gfxIns, 3f);
//Switch cameras
if (isLocalPlayer)
{
GameManager.instance.SetSceneCameraActive(true);
GetComponent<PlayerSetup>().playerUIInstance.SetActive(false);
}
Debug.Log(transform.name + " is DEAD!");
StartCoroutine(Respawn());
}
private IEnumerator Respawn ()
{
yield return new WaitForSeconds(GameManager.instance.matchSettings.respawnTime);
Transform _spawnPoint = NetworkManager.singleton.GetStartPosition();
transform.position = _spawnPoint.position;
transform.rotation = _spawnPoint.rotation;
yield return new WaitForSeconds(0.1f);
SetupPlayer();
Debug.Log(transform.name + " respawned.");
}
public void SetDefaults ()
{
isDead = false;
currentHealth = maxHealth;
//Enable the components
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = wasEnabled[i];
}
//Enable the gameobjects
for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
{
disableGameObjectsOnDeath[i].SetActive(true);
}
//Enable the collider
Collider _col = GetComponent<Collider>();
if (_col != null)
_col.enabled = true;
//Create spawn effect
GameObject _gfxIns = (GameObject)Instantiate(spawnEffect, transform.position, Quaternion.identity);
Destroy(_gfxIns, 3f);
}
private void Die()
{
audioPlayer.PlayOneShot(killStreakSounds[currentStreak]);
if (currentStreak < killStreakSounds.Length - 1) currentStreak++;
killTimer = streakReset;
}
void ResetStreak() // Call this when your character dies
{
currentStreak = 0;
killTimer = 0;
}
}
Comment
Answer by Map-Builder · May 28, 2020 at 11:57 PM
HI, hey that's not working, can you help me ?
Huge code incoming
:/
Sorry but...