Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sam_da_dev · Jan 09, 2021 at 04:50 PM · collisionunity 2dchild objectshieldhealth

How to make a parent object's collision not affect one of its child objects

Okay, so I am making a 2D game and I am trying to make a shield to block the enemy's bullet, but there is a problem, the shield is a child to the player object, and I have a script in the player object that says whenever the player gets touched by the enemy's bullet then the player would take damage, that's why whenever the enemy's bullet touches the shield, the player still takes damage, so I don't know what to do, if I un-child the shield from the player then it won't follow or rotate around the player


here's the player's health script

 public class PlayerHealth : MonoBehaviour
 {

     public int maxHealth = 100;
 
     public int currentHealth;
 
     public GameObject backgroundMusic;
 
     public GameObject theDeathEffect;
 
     public GameObject GameOverCanvas;
 
     public int DamageTaken = 20;
 
     public HealthBar healthBar;
 
     public GameObject Player;
 
     public GameObject Gun;
 
     public GameObject projectile;
 
     public GameObject RespawnSound;
 
 
     // Start is called before the first frame update
     void Start()
     {
         currentHealth = maxHealth;
     }
 
     // Update is called once per frame
     void OnTriggerEnter2D(Collider2D collision)
     {
         if(collision.CompareTag("EnemyBullet")) 
         {
             TakeDamage(DamageTaken);
 
             healthBar.SetHealth(currentHealth);
         } 
         
 
         if(currentHealth <= 0)
         {
             KillPlayer();
         }
     }
 
     void TakeDamage(int damage)
     {
         currentHealth -= damage;
     }
 
     void KillPlayer()
     {
         Time.timeScale = 0;
         GameOverCanvas.SetActive(true);
         Player.SetActive(false);
         Gun.SetActive(false);
         projectile.SetActive(false);
         RespawnSound.SetActive(true);
         ScoreScript.scoreValue = 0;
     }
 
 }




here's the enemy's bullet script

 public class Projectile : MonoBehaviour
 {
 
     public float speed;
     public GameObject CollisionEffect;
     
     private Transform Obstacles;
     private Transform player;
     private Vector2 target;
 
     // Start is called before the first frame update
     void Start()
     {
         player = GameObject.FindWithTag("Player").transform;
 
         Obstacles = GameObject.FindWithTag("Obstacles").transform;
 
         target = new Vector2(player.position.x, player.position.y);
     }
 
 
     void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
 
         if (transform.position.x == target.x && transform.position.y == target.y)
         {
             DestroyProjectile();
         }
     }
         void OnTriggerEnter2D(Collider2D other)
         {
             if(other.CompareTag("Player"))
             {
                 DestroyProjectile();
             }
             if(other.CompareTag("Obstacles"))
             {
                 DestroyProjectile();
             }
             if (other.CompareTag("Shield"))
             {
             DestroyProjectile();
 
             
             }
         }
         
         void DestroyProjectile ()
         {
             Destroy(gameObject);
             Instantiate(CollisionEffect, transform.position, Quaternion.identity);
         }
 
     }



help would be really appreciated

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by logicandchaos · Jan 10, 2021 at 01:01 PM

Make the player's collision a child object, you could call it player collision, then you can check for a shield collision or a player collision.

Comment
Add comment · Show 3 · Share
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
avatar image Sam_da_dev · Jan 10, 2021 at 07:52 PM 0
Share

okay ill try that

avatar image Sam_da_dev · Jan 11, 2021 at 05:14 PM 0
Share

thank you, it worked

avatar image FIREMAN040 · Jun 14, 2021 at 09:23 PM 0
Share

@logicandchaos sorry it's a bit late from when you posted this, but I'm trying to do the same thing and can't figure out how. Could you post an example for me?

avatar image
0

Answer by CmdrZin · Jan 10, 2021 at 08:11 PM

You could set the Player collider to disable when carrying the shield and have the shield collider big enough to cover the Player. Lots of options including @logicandchaos's.

Comment
Add comment · Share
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

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

183 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 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

Take health from enemy 3 Answers

Fired projectile collision with player issue 1 Answer

Creating ship energy shields effect 2 Answers

Creating a Asteroid health script 5 Answers

How to send collision points to the physics engine? 2 Answers


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