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 cparki3 · Nov 07, 2014 at 02:03 PM · collisioncollideroverlapsidescroller

Trigger 2D repel on overlap (2-D player combat issue)

Hey guys,

First real question on the forums here. I am working on a combat system similar to that of Smash brothers brawl and I cam upon an interesting issue. In Brawl, characters can't occupy the same space but they also can't stand on each others heads (exactly what I want). I have tried several options including triangular polygon colliders (a bit hacky) to my most recent attempt of having a 2-D box collider that isn't a trigger to prevent pushing through characters with another box collider that is a trigger. This trigger detect overlap and pushes the character off depending on which way he is facing.

I think the ideal situation is to have two box colliders that are triggers and also repel each other on contact or overlap. I have messed with this as well and I can get them to repel from side to side but when you jump on a character it "jumps" to the edge of the collider and isn't a smooth transition.

Sorry for the rant but if anyone has a solution, I would very much appreciate an opinion.

Please let me know if you need any additional information.

Thanks!

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

Answer by Bunny83 · Nov 07, 2014 at 02:15 PM

Well, the easiest way is to just push the one that's above the other side ways (and a little up). If you use rigidbodies, just use normal colliders and if you collide with another player just do something like this:

 // C#
 float force = 20f; // adjust to your needs
 
 void OnCollisionEnter2D(Collision2D col) 
 {
     if (col.gameObject.tag == "Player")
     {
         var rel = rigidbody2D.position - col.rigidbody.position;
         if (rel.y > 0.5f) // if we are over the other
             rigidbody2D.AddForce(rel*force, ForceMode2D.Impulse); // push us away from the other player
     }
 }

ps: don't ask for "opinions" here ;) This is NOT a forum.

edit
Just re-read your question. If you don't want a to "agressive" push you can use

 float force = 20f; // adjust to your needs
 void OnCollisionStay2D(Collision2D coll)
 {
     if (col.gameObject.tag == "Player")
     {
         var rel = rigidbody2D.position - col.rigidbody.position;
         if (rel.y > 0.5f)
         {
             rel.y = 0; // If you don't want to push him upwards.
             rel.Normalize();
             rigidbody2D.AddForce(rel*force);
         }
     }
 }

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
avatar image
0

Answer by cparki3 · Nov 13, 2014 at 06:59 AM

Thanks a lot for the help. I ended up using a modified version of the script you provided because I wanted them to be able to pass through each other if enough force was applied. Here is what I ended up using:

 public float force = 250f;
     
     void OnTriggerStay2D(Collider2D coll)
     {
         if (coll.gameObject.tag == "RepelBox")
         {    
             Rigidbody2D thisBody = GetComponentInParent<Rigidbody2D>();
             Rigidbody2D thatBody = coll.GetComponentInParent<Rigidbody2D>();
             var rel = new Vector2(thisBody.position.x, 0) - new Vector2(thatBody.position.x, 0);
             
             rel.Normalize();
             thisBody.AddForce(rel*force);
         }
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hazards not Working Properly 1 Answer

Collider.OnCollisionStay Climb up/Down a ladder? 0 Answers

Knockout Or Suicide Detection 1 Answer

Unity Colliders are Overlapping/Intersecting 2 Answers

Can Camera.WorldToScreenPoint be used to check if one collider is over another in screen space? 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