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 N7D · Nov 24, 2017 at 10:16 PM · colliderdamageghosting

Ghost through enemy for a short duration after being damaged

I have AI enemies that chase player and when they collide with player, player gets damage and for a short duration player does not get extra damage and I also want player to ghost through enemies while effect takes place.


Both player and enemies got Colliders on them.

Player got tag "player" and layer "player"

Enemies got tag "NPC" and layer "NPC"

"NPC" and "player" layers both collide in collision matrix.


What is the good way to achieve this ghosting effect?

The method in which this should be performed.

 void Damaged(DamagePackage damagePackage)
     {
         if (tag == damagePackage.OwnerName)
         {
             return;
         }
         if (CompareTag("Player"))   
         {
             if (!(Time.time > _nextRecieveDamage)) return;
             _nextRecieveDamage = Time.time + 0.1f;
         }
         HP -= damagePackage.Damage;
     } 

Comment
Add comment · Show 1
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 N7D · Nov 24, 2017 at 10:23 PM 0
Share

I find this answer pretty similar to $$anonymous$$e, but I still want to know a better solution than creating different layer. And also interested to hear how does it implemented in other games, it is pretty usual thing in 2D platformers.

https://answers.unity.com/questions/315409/dynamic-collision-layers.html

1 Reply

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

Answer by ShadyProductions · Nov 25, 2017 at 01:16 AM

You can cache any collision with any type of enemy that way you can check the cache on collision to see if we have already collided with this type of enemy, if not then we apply the action we want to happen and push the enemy to the collided cache, where as a certain amount of time passes we remove it from the cache. The reason for allowing cache of multiple types of enemies is so that we can apply different effects to different kind of enemy types if we have to. Something like:

     private HashSet<int> _collissionCache = new HashSet<int>();

     void Damaged(DamagePackage damagePackage, int entityId)
     {
         if (tag == damagePackage.OwnerName)
         {
             return;
         }

         // Check if we have this entity in our cache
         if (_collissionCache.Contains(entityId))
         {
             // Check if the time is finished
             if (Time.time > _nextRecieveDamage)
             {
                 // Remove from cache and continue the method.
                 _collissionCache.Remove(entityId);
             }
             else
             {
                 // If time is not done we add next receive damage time and return.
                 if (CompareTag("Player"))
                     _nextRecieveDamage = Time.time + 0.1f;

                 return;
             }
         }

         // Apply the effect
         HP -= damagePackage.Damage;

         // Add entity to cache
         _collissionCache.Add(entityId);
     }
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 N7D · Nov 25, 2017 at 05:25 PM 0
Share

And how can I prevent collision between player and that entity then?

avatar image ShadyProductions N7D · Nov 25, 2017 at 09:00 PM 0
Share

You don't need to prevent it, it will only add the entity to the cache if it's not collided before. You just have to sort the ti$$anonymous$$g stuff out.

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

85 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

Related Questions

How to make my character die 1 Answer

Attack combos and collision detection 0 Answers

This collision Enemy health code suddenly stopped working. 1 Answer

Damage Animation doesn't play 1 Answer

Two objects with different damage - Not working? 0 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