Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bssnator_unity · Mar 03, 2018 at 05:50 PM · colliderscript.destroytriggersbullets

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My bullets destroys Objects without it hitting it. 1 Answer

Destroying objects within collision 1 Answer

cant destroy other object 1 Answer

FPS walking down stairs cant walk back up again 0 Answers

How to make a particle system play on trigger with another box collider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges