After taking damage, the player is invulnerable and cannot shoot for a few seconds. The avatar blinks (renderer on and off) to reflect that state. This typically works fine.
However, when the player shoots at an enemy, gets hit, and the shot connects after receiving damage, the renderer and shooting are permanently set to the off position. The on/off code does not execute, and the player is never able to shoot again. As you can imagine, this is fairly game breaking. Any ideas? I’ve been struggling with this for the past 2 months or so.
Code for taking damage (decrease means health decrease)
static function Decrease(){
var hs:GameObject=GameObject.Find("HealthSmall"); //health indicator
var pShooting = FindObjectOfType(typeof(PlayerShootingJS)); //Shooting script
if (playerInvulnerable == 0){ //Invulnerable = 0 means that the player is not invulnerable
if (PlayerHealth < 10){ //Player knockback on damage
if (m.transform.localScale.x > 0){
m.transform.position.x = m.transform.position.x - 3;
}else if(m.transform.localScale.x < 0){
m.transform.position.x = m.transform.position.x + 3;
}else{
m.transform.position.x = m.transform.position.x - 3;
}
PlayerHealth--; //Reduce player health
}
hs.renderer.enabled = false; //reduce health indicator
//Player invulnerable after taking damage. Blink player image.
//Currently not working. Glitches when player kills enemy while invulnerable. Freezes renderer in off state and maintains permanent invulnerability.
playerInvulnerable = 1; //Sets player to invulnerable
pShooting.enabled = false; //Disables player's ability to shoot
m.renderer.enabled = false; //blink code starts here
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.1);
m.renderer.enabled = true;
yield WaitForSeconds (0.1);
m.renderer.enabled = false;
yield WaitForSeconds (0.2);
m.renderer.enabled = true;
yield WaitForSeconds (0.2);
m.renderer.enabled = false;
yield WaitForSeconds (0.2);
m.renderer.enabled = true;
yield WaitForSeconds (0.2);
m.renderer.enabled = false;
yield WaitForSeconds (0.2);
m.renderer.enabled = false;
yield WaitForSeconds (0.2);
m.renderer.enabled = true;
yield WaitForSeconds (0.2);
m.renderer.enabled = false;
yield WaitForSeconds (0.2);
m.renderer.enabled = true; //Player is now fully visible, no more blinking
playerInvulnerable = 0; //Removes invulnerability
pShooting.enabled = true; //Player can shoot
}
}
Code for enemy taking damage
var Health = 10; //EnemyHealth
function OnTriggerEnter (other: Collider) {
if (other.tag=="Brainwave"){ //Brainwave is the projectile
var bw:GameObject=GameObject.Find("playerProjectile");
Health--; //Reduce health of enemy
Destroy (GameObject.FindWithTag("Brainwave")); //Destroys projectile
PlayerHealth.score = PlayerHealth.score + 125; //Increases score
}
if (other.tag=="Player"){
PlayerHealth.Decrease(); //Calls function that does damage to player and makes him invulnerable
}
}
function Update(){
if (Health <=0){ //If enemy health is zero or less, destroy enemy
Destroy(gameObject);
}
}
Code for projectile shooting
var projectile : Rigidbody;
var speed = 10; //projectile speed
private var allowfire : boolean = true; //whether or not the player can shoot. Adds a cooldown between shots
function Update () {
if ( Input.GetButton ("Fire1")(allowfire)) { //Press fire button, call Fire function
Fire();
}
}
function Fire(){
var m:GameObject=GameObject.Find("MainChar"); //sets m to MainChar; Reference
allowfire=false; //Shooting cooldown
var projectileStartPosition = m.transform.position; //Sets projectile start position
projectileStartPosition.y += .4;
clone = Instantiate(projectile, projectileStartPosition, m.transform.rotation); //creates new projectile
if (m.transform.localScale.x > 0){ //Sets velocity and direction
clone.velocity = new Vector3(speed,0,0);
}else if(m.transform.localScale.x < 0){
clone.velocity = new Vector3(-speed,0,0);
}else{
clone.velocity = new Vector3(speed,0,0);
}
Destroy (clone.gameObject, 0.3); //Removes the projectile from the scene
yield WaitForSeconds(0.3); //Reduces firing speed
allowfire=true; //Shooting enabled again
}