Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

unity3d - How do I detect which side of the object I exited from on OnCollisionExit2D()

I can detect which side of an object I collide with however I need to reset some variables when I exit a collision. How do I tell which side of an object I exited from? (ie if I have collided with the ground then I collide with a wall on the left but then move right I need to know if I have exited from the collision with the wall or if I exited from the collision with the ground.)

Note the blocks I am colliding with are both walls and ground so I can't use layer names etc.

question from:https://stackoverflow.com/questions/65852884/how-do-i-detect-which-side-of-the-object-i-exited-from-on-oncollisionexit2d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1.

I really struggle to understand the question, but a common trick is you actually have a number of colliders on the object.

Say it is a cube.

Instead of having one cube-shaped collider, in fact have SIX colliders!

(So, basically flat ones, whatever technique you want to use.)

Depending on your situation, you then know exactly what collided with what.

I really don't understand the question, but I think you're wanting to know, of the six, which was the "last one" touched.

Of course, that is easy to achieve as a basic programming matter, just figure out which of the six was touched last.?

I hope it helps, sorry I have not much clue what you are describing.

2.

Note that as BugFinder explains. As you "leave", simply look at your new velocity, that is to say look at the direction of your velocity.

Obviously, that will instantly tell you "what part" of the other object, you must have bounced off.

Note too that indeed you can simply cast backwards from your new velocity, and see what the hell you hit - that is likely what bounced you!

--

? beware that PhysX can matt things in to the same frame, the idea of what was "touched last" when you're in a corner can be problematic. This is why "2" is a common approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...