alright you guys can ignore everything but the variables about the respawn system. I set it so that when i die it should wait 10 seconds instead it waits only 3, and if i died for the second time it doesnt count down at all and i instantly respawn. Plz tell me whats wrong with the code or is it a bug in unity?
using UnityEngine;
using System.Collections;
public class Player : Entity {
public int level;
private float experienceToLevel;
private float currentLevelExperience;
public Transform spawnPoint;
public int totalMoney;
public CharacterController playerPrefab;
private GameGUI gui;
private bool shouldCountdown;
private PlayerController playerController;
private bool dieTimer;
private float dieTimerDelay = 20;
private bool shouldRespawn;
private ShopGUI shopGUI;
void Start(){
playerController = gameObject.GetComponent<PlayerController>();
dieTimer = false;
shouldCountdown = false;
shopGUI = GameObject.FindGameObjectWithTag ("shop").GetComponent<ShopGUI> ();
gui = GameObject.FindGameObjectWithTag ("GUI").GetComponent<GameGUI>();
LevelUp ();
gui.SetPlayerHealth (health);
}
void Update(){
if(dieTimerDelay<0){
dieTimerDelay =0;
}
if(health <=0){
shouldCountdown = true;
Dead();
}
if(health >0){
shouldCountdown = false;
dieTimerDelay = 10;
}
if (shouldCountdown == true) {
dieTimerDelay = 10;
//Debug.Log(dieTimerDelay);
dieTimerDelay -= Time.time;
}
if(dieTimerDelay <=0){
shouldRespawn = true;
}
if(dieTimerDelay >0){
shouldRespawn = false;
}
Debug.Log ("Should i respawn: "+shouldRespawn);
}
public void Dead(){
totalMoney = totalMoney / 2;
transform.position = new Vector3(-7.5f,0.6f,-3);
playerController.walkSpeed = 0;
playerController.runSpeed = 0;
playerController.canShoot = false;
if(dieTimerDelay>0){
shouldRespawn=false;
}
if(dieTimerDelay<=0){
shouldRespawn = true;
}
if(shouldRespawn == true&&dieTimerDelay<=0){
dieTimerDelay = 10;
Debug.Log(shouldRespawn +" / "+dieTimerDelay);
Respawn ();
}
}
private void Respawn(){
transform.position = spawnPoint.transform.position;
playerController.walkSpeed = 5;
playerController.runSpeed = 8;
playerController.canShoot = true;
health = 100;
dieTimerDelay = 10;
shouldRespawn = false;
shouldCountdown = false;
gui.SetPlayerHealth (health);
}
public void AddExperience(float xp){
currentLevelExperience += xp;
if(currentLevelExperience>=experienceToLevel){
currentLevelExperience -= experienceToLevel;
LevelUp();
}
gui.SetPlayerXP (currentLevelExperience / experienceToLevel, level);
Debug.Log (currentLevelExperience / experienceToLevel);
}
public void AddMoney(int moneyToAdd){
totalMoney += moneyToAdd;
gui.SetPlayerMoney (totalMoney);
}
public void AdjustCurrentHealth(float adj){
health -= adj;
gui.SetPlayerHealth (health);
}
private void LevelUp(){
level++;
experienceToLevel = level * 50 + Mathf.Pow (level * 2, 2);
AddExperience (0);
}
}