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 Internetman · Mar 14, 2016 at 06:46 PM · c#2dcollisiononcollisionentershield

Collision issue with SetParent (C#)

I'm developing a 2D game where I have a power up that sets a shield as parent on the player. The shield has it's own tag. But I have a problem, once the player gets the shield, I set it as parent and once an enemy collides with the player and the shield the player dies, making it seem that the player didn't have a shield at all.

I've set the enemies so that they should collide with a "Player" tag and kill the player, but i want to have it so that when the shield is in use, the enemies is going to collide and "bounce" off. Should I ignore the players collision when the player has a shield or is there some other way to solve this? Keep in mind that the enemies move with a speed, so could it be that the enemies move too fast so they still collide with the "Player" tag?

I'm grateful for all the help I can get, you guys are great!!

Here's the code for spawning shield:

 public void SpawnShield()
     {
         // Get the transform ahead of time for readability
         Transform charTransform = GetComponent<MoveScript>().character.transform;
         
         // Instantiate and keep track of the new instance
         GameObject newShield = Instantiate(shield, charTransform.position, charTransform.rotation) as GameObject;
         
         // Set parent
         newShield.transform.SetParent(charTransform);
 
         shieldPowerUpActivated = true;
 
     }

and here is the enemy collision code:

 public void OnCollisionEnter2D(Collision2D col)
     {
         //If the enemy collides with "Player" load level
         if(col.gameObject.tag == "Player")
         {
             Application.LoadLevel(0);    
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoeStrout · Mar 14, 2016 at 06:55 PM

Yes, an object (especially a fast-moving one) may easily collide with both the shield and the player inside it at the same time. Your player-collision code should simply check whether the shield is currently in effect, and if so, ignore that collision.

Comment
Add comment · Show 2 · 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 Internetman · Mar 14, 2016 at 08:34 PM 0
Share

Hm, seems I still can't fix the problem, I think there might be an issue because I set parent for the shield, I scaled up the collision box for the shield and when an enemy hit, but the player was still killed. So I don't think it's the speed of the enemy that causes it.

avatar image JoeStrout Internetman · Mar 14, 2016 at 10:15 PM 0
Share

No, it's your code that is killing the player. The speed of the enemy relates (weakly) to whether the collision event fires. If that's confusing, ignore it and assume that the collision event is always going to fire on both the player and (when present) the shield. So, you need to update your code so that if the shield is in effect, it doesn't kill the player — it just ignores the collision.

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

128 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

Related Questions

make OnCollisionEnter2D ignore collisons of child objects 0 Answers

Checking for collision with 2D objects/sprites 3 Answers

Collision on specific Frames 1 Answer

Multiple Cars not working 1 Answer

[c#]collision script not working 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