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 cyfrostan · Nov 15, 2015 at 08:14 AM · collisioncollidercolliderscollision detection2d-physics

How to reliably break contact/collision?

I'm making a top down game in a style similar to the original Legend of Zelda. I wanna put in a mechanic that would prevent enemies from getting the player stuck in a corner.

The idea is to upon contact disable collisions between player and enemies for a brief period. The code I have can prevent future collisions between player and enemy, but it doesn't disable the triggering collision.

Basically for as long as the player and the enemy are touching each other they remain corporeal to each other. Any advice on how to reliably break a specific collision or break contact? Or maybe design better around this issue?

Relevant player code:

 public void ApplyDmg(int dmg){
         if(!isImmune)
             HP -= dmg;
         if (HP <= 0)
             gameObject.SetActive (false);
         else
             StartCoroutine(PlayerImmunity (immunityDuration, 0.2F));
     }
 
     IEnumerator PlayerImmunity(float duration, float blinkTime) {
         isImmune = true;
         float immunityTime = Time.time + duration;
         Physics2D.IgnoreLayerCollision (playerLayerID, enemyLayerID);
         while (immunityTime > Time.time) {
             
             //toggle renderer
             renderer.enabled = !renderer.enabled;
             
             //wait for a bit
             yield return new WaitForSeconds(blinkTime);
         }
         
         //make sure renderer is enabled when we exit
         renderer.enabled = true;
         Physics2D.IgnoreLayerCollision (playerLayerID, enemyLayerID, false);
         isImmune = false;
     }

Relevant enemy code:

 void OnCollisionEnter2D(Collision2D other){
         if (!isStunned && other.gameObject.tag == "Player"){
             other.gameObject.SendMessage ("ApplyDmg", 1);
             ApplyStun(0.3F); //Applies a brief stun to itself
             Debug.Log ("Register hit Player");
         }
     }

Comment
Add comment · Show 4
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 instruct9r · Nov 15, 2015 at 01:00 PM 0
Share

Does your Enemy or Player have RigidBody? If Yes does some of them, have a child object, that is assigned to different Layer (Like Default) and have a Collider Component?

avatar image cyfrostan instruct9r · Nov 15, 2015 at 02:58 PM 0
Share

Yes, both of them have a RigidBody2D component.

Yes, Player object does have a couple of child objects assigned to Default layer. Namely the AttackColliders, they each consist of a Transform and a BoxCollider2D components, all of which are triggers. They are used for hit detection. The BoxCollider2D components remain disabled by default, being enabled upon attack for exactly 1 frame to detect collisions.

I just tried changing those to the Player layer, but the issue still remains the same. Besides, doing that would mean that player immunity also disables player attacks, would it not?

avatar image instruct9r cyfrostan · Nov 15, 2015 at 04:46 PM 0
Share

Usually if you have a gameObject with RigidBody and child with colliders, Unity is considering those as Compound Colliders. That coud be the reason, why you are still registering the trigger, between the Player and Enemies.

Other than that, if the collision is turned Off, when you IgnoreLayerCollision, there shoud be no trigger registered between these 2 colliders. (If i understood correctly, this is what happens in your case, Collision is Ignored, but you still get a trigger registration)...

Try debugging, what's the name of the object that's getting triggered...

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Strange issue with collider 1 Answer

Detect if a non-trigger collider is inside another collider 1 Answer

How to detect collision between 2 objects while checking are they the ones that need to collide? 1 Answer

How to get OnTriggerEnter effect without using isTriggered on Collider? 1 Answer

Why there is no Collider.IsTouching(...) ? 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