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 coreyog · Dec 28, 2017 at 07:55 AM · 2dcollider2d

Correcting for a collision overlapping too much?

I have a TileMap with a TileMapCollider2D and a Sprite with a BoxCollider2D. I'm not using physics at all so I'm correcting for the collision on my own. Here's my attempt:

 void FixedUpdate() {
     float h = Input.GetAxis("Horizontal") * Time.deltaTime * this.Speed;
     float v = Input.GetAxis("Vertical") * Time.deltaTime * this.Speed;
     Vector3 pos = this.transform.position;
     pos.x += h;
     pos.y += v;

     foreach (Tilemap map in this.Maps) {
         TilemapCollider2D mapc = map.GetComponent<TilemapCollider2D>();
         ColliderDistance2D dist = this.boxCollider.Distance(mapc);
         if (dist.isOverlapped && dist.isValid) {
             Debug.DrawLine(dist.pointB, dist.pointA, Color.white);
             Vector2 correction = dist.normal * dist.distance;
             pos += (Vector3)(correction);
         }
     }

     this.transform.position = pos;
 }

This logic is on the blue square in this gif:

animation of failed collision correction

According to the documentation, dist.normal * dist.distance should be exactly the distance and direction required to make the two colliders touch. However you can see that I can push a few pixels into the red walls. When I release the button it takes several frames to back out of the wall. It goes a little haywire too when I go diagonally into the corner.

correction.gif (43.6 kB)
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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nikaas · Dec 29, 2017 at 08:30 AM

TLDR: Insert this.transform.position = pos; at line 17 (just before the foreach loop).


When i had such problems it always was code reordering issue. To me it looks like it is because you are adding to the pos vector and applying it as current position at the end. But you are calculating the collision according to boxCollider's last/previouse frame (i.e. h and v are not taken into account) . In other words your collision check is one frame late - you check if there was collision last frame and apply the movement after that.

You should apply pos as transform.position before calculating ColliderDistance2D (or find a way to check against pos itself).

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 Nikaas · Dec 29, 2017 at 10:25 AM 0
Share

Or you can try to apply pos only if there is no collision/overlapping.

avatar image oakus · Jul 24, 2020 at 12:39 PM 0
Share

Thank you for the answer. I just had the same problem and turned out it was because of this.

avatar image
0

Answer by theterrificjd · Dec 28, 2017 at 01:34 PM

Long shot answer attempt:


Usually Distances formulas are usually from centers of the transforms, meaning approaching at non-perpendicular angles produce different distances.

I think you may want something to do with Bounds properties of the colliders instead.


I've only used physics so far, and haven't done this personally, but I think a few of the Bounds functions look useful. My thinking is the Intersects / ClosestPoint in combination (if the objects bounds intersects the wall's boundes, adjust the objects closest point to

https://docs.unity3d.com/ScriptReference/Bounds.html

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 coreyog · Dec 28, 2017 at 10:18 PM 0
Share

The short white line you see in the gif is me illustrating pointA and pointB given by the distance formula. They don't appear to be co$$anonymous$$g from the middle of the colliders. However switching to using the bounds may be a better solution, I'll try it and report back.

avatar image coreyog coreyog · Dec 29, 2017 at 03:47 AM 0
Share

Bounds are way too generic. It refers to a rectangle encompassing EVERY Tile collider not EACH Tile collider.

avatar image
0

Answer by JusSumGuy · Dec 28, 2017 at 12:57 PM

I had this problem too with a game I was designing. I'm looking forward to seeing the answer to this one , because I fixed it with a not so elegant approach. But I'm pretty sure it has something to do with how the physics components are interacting with each other. When you add force to the collider it keeps pushing towards the other collider that's why you have that little overlap with a reversed force. In the corner you get the haywire because now the opposite force is pushing back from two directions. Hope this helps lol, but thanks for asking the solution will help me out as well :).

Comment
Add comment · Show 1 · 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 coreyog · Dec 28, 2017 at 10:13 PM 0
Share

I'm foregoing the physics of Unity. It's a top down game that wouldn't benefit from a physics engine so I'm not applying forces or adding rigid bodies. I'm only using colliders because of how the Tile$$anonymous$$ap can automatically construct colliders around the sprites of tiles. I'm manipulating transform.position directly.

avatar image
0

Answer by BOB_KSE · Dec 28, 2017 at 06:59 PM

It's happening because the player is trying to force its way into the collider on that frame and on the next frame collision is detected and the player is pushed back. I have a not so elegant solution.


when the player hits the "red walls" using "uparrow" for example. just take away user's ability to press "uparrow" anymore, using boolean variables and OnCollisionStay2D() function and OnCollisionExit2D() give back control. I have never tried it though. I am suffering from the same problem, but left it as it is.


https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay2D.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionExit2D.html

Comment
Add comment · Show 1 · 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 coreyog · Dec 28, 2017 at 10:16 PM 0
Share

So far I'm not using any of the OnCollisionX events because I want to respond to the collision the same frame it happens (before drawing so you never actually see the overlap happen) and nudge the player back just enough so the player is resting against the wall. The movement of the player and the nudging back of the player are currently happening at the same frame so it shouldn't be possible for the player to ever be drawn overlapping the wall collider but apparently my nudge back isn't working how I'd like.

avatar image
0

Answer by coreyog · Dec 29, 2017 at 05:23 AM

For now, I've added a RigidBody2D and tweaked some of the Project Settings for Physics2D until I have what I want. It still presses into the walls but not nearly as much as before. You can't notice it. Also, because of the slight pressing into walls, I've given the BoxCollider2D a smaller size and a radius. The rounded corners prevent it from catching on walls that are flush with each other.

rigid body correction

I didn't realize that a rigid body would reject collisions even if I'm not applying forces. This may not be the optimal solution since it's using the physics engine to achieve the push back but it's getting me the results I'm after. I'll over-optimize later if this has a noticeable impact on performance.


rigidbody-correction.gif (207.5 kB)
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 coreyog · Dec 29, 2017 at 05:24 AM 0
Share

BTW, I'm not accepting this as the answer because I'm hopeful for a non-physics approach like I was trying before. $$anonymous$$aybe if this goes long enough without a solution I'll accept this.

avatar image JusSumGuy · Dec 29, 2017 at 09:53 AM 1
Share

Cool, that's a pretty good solution! I'll keep this in $$anonymous$$d. And hey if you ever come across a situation where you need your object to keep vibrating side to side you already know how to do that as well. Usually I find the cool tricks by mistake lol.

avatar image BOB_KSE · Dec 30, 2017 at 07:11 AM 0
Share

don't stop your project because of the inconvenience. you will surely find a solution later somewhere. happy developing :)

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

141 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

Related Questions

Unity2D - OnTriggerEnter2D not working 1 Answer

Using child colliders with rigidbodies/joints in 2D 0 Answers

Problem with method Collider2D.isTouchingLayers() 4 Answers

Find next corner of a 2d pollygon collider 0 Answers

Why is there a gap between my player and the wall during wall slide? SOLVED 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