Should be a fairly simple problem, but it is giving me some trouble.
I’ve got some “2d” rectangular units with trigger boxes being moved around using the translate function. When they collide I need them to stop dead, but they should still be able to freely move away from each other and parallel to the other unit’s edge.
Related questions have suggestions like “use triggers” which I am already doing.
My current solution is to maintain an old position from a fraction of a second ago and revert to that position OnTriggerEnter and OnTriggerStay. This results in some unsightly jerkiness, and worst of all, the tendency of two moving units to become stuck together.
How do you get objects to stop dead when colliding without sticking or jerking?
I ended up adding four long triggers. One to each side of the unit as children of the unit just outside the units main collision. OnTriggerEnter, these triggers stop any movment by the unit in the direction of that trigger.
So if the front trigger enters a trigger, then the main movement script gets set to something like:
horazontalMove = Mathf.Min(0,horazontalAxis)
Or if the back trigger enters a trigger then it has the main script do something like:
horazontalMove = Mathf.Max(0,horazontalAxis)
I would still like to know a solution that relies more on code and less on strange uses of the tool, but for now this works fine.
Or if you’re using triggers (which can’t collide physically), you need to manually locate where they should be.
For instance, to force me and him apart such that we’re exactly touching (where xWidth is the distance from the center point to the edge along the X axis):