This site uses strictly necessary cookies. More Information

X- Home /

**Question**by Bonfi_96 · Mar 17, 2016 at 12:45 AM · resolutioncharacter controllerdetectioncollsionpushback

# Collision resolution in tight spaces

I'm trying to implement collision detection on my character controller so I'm using the pushback method. Everything works fine except when I have two colliders that are placed in a similar way to the image, making a really tight "angle". Those two colliders could be two box colliders, or any other kind of colliders, the issue is that, after applying the pushback vectors the player collider is still overlapping with one or both of the "walls". This results in a sort of jittery back and forth movement as the script solves the collisions frame by frame until there are no more collisions.

Here's an awesome ms paint drawing that sums up the situation.

Is there a better way to solve this kind of collisions? A quick work around would be to repeat the collision detection more times per frame untill the collision is completely solved but this doesn't sount to me as a good solution.

Thanks!

**Answer** by Bunny83
·
Mar 17, 2016 at 04:54 AM

Well, when adding all correction vectors with their proper magnitude together you get a new correction vector that has the correct direction, but the wrong magnitude. To get the correct magnitude you simply project the combined correction vector onto one of the original correction vectors. The magnitude of that projected vector would be smaller than the initial calculated magnitude that is needed to push the object away from that contact point. The ratio between the magnitude of the initial correction vector and our projected vector is the amount you need to scale the combined correction vector.

This should work in theory:

```
Vector3 A; // left correction vector
Vector3 B; // right correction vector
Vector3 C = A+B // combined correction vector
Vector3 P = Vector3.Project(C, A); // projected vector
C = C * A.magnitude / P.magnitude;
```

This also corrects the magnitude if you have a wide angle where the correction would be too large instead of too small.

Just to explain why this is necessary:

If the angle between your two walls is smaller than 90° the partially cancel each other out since the vectors point in opposite directions.

If the angle between the walls is greater than 90° the vectors actually accumulate partially since they now partially point in the same direction.

Thanks, I'll try this out with code as soon as I can. How can I adapt with more than two colliders? Is it enough to project the C=A+B+D+.... on any of these vectors? I understand your explaination about vectors cancelling eachother out but I still can't get why the magnitude ratio is the one that solves the problem (for acute angles P's magnitude is smaller than A's so the ratio is >0, therfore the player gets pushed back more preventing other intersections)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.