- Home /
Making a bullet invinceble
Hi!
I am trying to make the enemy bullet invincible and kind of pass right through the player bullet (Kind of enemy bullet eating the player bullet on it's path). I have 5 different bullets that both the player and enemy can shoot. YellowBullet, RedBullet, BlueBullet, GreyBullet and PurpleBullet each with a tag with their name.
Right now in my script I tried to set up a Boolean for the OnTriggerEnter method but not sure how I would make only the player bullet destroy and make the enemy one survive and pass through.
Here is my bullet script:
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
public float damage;
public bool willDestroy = false;
public float GetDamage(){
return damage;
}
public void Hit(){
Destroy (gameObject);
}
public void DestroyBullets(){
willDestroy = true;
}
public void NullDestroyBullets(){
willDestroy = false;
}
void OnTriggerEnter2D(Collider2D collider) {
if(willDestroy = true){
Destroy (gameObject);
NullDestroyBullets();
}
}
}
Player Script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Player Bullets
public GameObject bullet;
public float bulletSpeed;
public Projectile invincibleBullet;
// Player Health Slider
public static float currentHealth = 50f;
public static float maxHealth = 50f;
private Slider playerHealthSlider;
void Start(){
playerHealthSlider = GameObject.FindGameObjectWithTag ("PlayerHealthSlider").GetComponent<Slider>();
playerHealthSlider.value = calculateHealth();
}
void Update () {
playerHealthSlider.value = calculateHealth();
// (Input.GetMouseButtonDown(0) && GUIUtility.hotControl==0)
if(Input.GetKeyDown(KeyCode.Mouse0)){
GameObject playerBullet = Instantiate(bullet, new Vector3(0f,-13.5f,0f), Quaternion.identity) as GameObject;
playerBullet.rigidbody2D.velocity = new Vector3(0,bulletSpeed,0);
playerBullet.transform.parent = transform;
}
if(currentHealth <= 0){
Destroy (gameObject);
}
}
float calculateHealth(){
return currentHealth / maxHealth;
}
public void TakeDamage(){
currentHealth--;
}
}
And Enemy Script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class RedEnemyScript : MonoBehaviour {
// Reference.
public EnemySpawner spawnScript;
public EnemyAttackScript enemyAttack;
public RedXpScript redXp;
// Health.
public float currentHealth;
public float maxHealth;
// Bullets.
public Projectile invincibleBullet;
public GameObject bullet;
public float bulletSpeed;
// Slider.
private Slider enemyHealthSlider;
private SliderColor healthColor;
void Start(){
// Enemy Health SLider
enemyHealthSlider = GameObject.FindGameObjectWithTag ("EnemyHealthSlider").GetComponent<Slider>();
enemyHealthSlider.value = calculateHealth();
// Slider Color
healthColor = GameObject.Find("Enemies").GetComponent<SliderColor>();
healthColor.RedColor();
}
void Update(){
enemyHealthSlider.value = calculateHealth();
FullHealth ();
if (currentHealth <= 0){
enemyAttack.ResetCountdown();
redXp.XpIncrease();
Destroy(gameObject);
spawnScript.Spawn();
}
}
float calculateHealth(){
return currentHealth / maxHealth;
}
void FullHealth(){
if(currentHealth >= maxHealth){
currentHealth = maxHealth;
}
}
public void Attack(){
invincibleBullet.DestroyBullets();
GameObject enemyBullet = Instantiate(bullet, new Vector3(0f,36.5f,0f), Quaternion.identity) as GameObject;
enemyBullet.rigidbody2D.velocity = new Vector3(0,bulletSpeed,0);
enemyBullet.transform.SetParent (GameObject.FindGameObjectWithTag("Enemies").transform, true);
}
void OnTriggerEnter2D (Collider2D collider){
Projectile bullet = collider.gameObject.GetComponent<Projectile>();
if(collider.gameObject.tag == "YellowBullet"){
currentHealth = currentHealth - bullet.GetDamage();
bullet.Hit ();
enemyAttack.EnemyAttack();
} else if(collider.gameObject.tag == "RedBullet"){
currentHealth = currentHealth + bullet.GetDamage();
bullet.Hit();
enemyAttack.EnemyAttack();
} else if(collider.gameObject.tag == "BlueBullet"){
currentHealth = currentHealth - bullet.GetDamage()*2;
bullet.Hit ();
enemyAttack.EnemyAttack();
} else if(collider.gameObject.tag == "GreenBullet"){
currentHealth = currentHealth - bullet.GetDamage()/2;
bullet.Hit ();
enemyAttack.EnemyAttack();
} else if(collider.gameObject.tag == "GreyBullet"){
currentHealth = currentHealth - bullet.GetDamage()/2;
bullet.Hit ();
enemyAttack.EnemyAttack();
} else if(collider.gameObject.tag == "PurpleBullet"){
currentHealth = currentHealth - bullet.GetDamage()*2;
bullet.Hit ();
enemyAttack.EnemyAttack();
}
}
}
Comment