OnCollisionEnter / Exit problems

Hello, I am a newbie Unity programmer, and I have some issues handling collisions. I hope that my problem is not solved in other thread, since I don’t want to replicate post, but I haven’t found anything after a big search.
For the record, my project is a 2D plataformer in which I have for now a player object, which has a box collider and its marked as kinematic (so I can handle the collisions). My other objects are a set of rectangles that act as ground platforms, which are rigid bodys. All goes well, and I handle the collisions with OnCollisionEnter and OnCollisionExit when the player is in contact with a platform, but the problem comes when the player is in contact with not one but two platforms. They are slightly superimposed, in order to avoid gaps between the platforms, and when the player is in contact with two of them, the OnCollision events go crazy and are called on each frame, leaving the game in a instable state, and ultimately, not working.

Just for the record again, I have a “sticky” distance, which a small distance that my player goes into the platforms, since I realized that if the distance between objects (when a player is in top of a platform) was 0, the collision events were called every frame, and this avoided the problem al lest for one simultaneous collision. Don’t know if this is the correct approach anyway.

bump

A couple things you could do to solve this issue.

One, have a platform manager. Use OnCollision/OnTrigger to call a function in the platform manager. Inside the platform manager perform a check before you run the function - If(!isRunning).

Two, have a check in the player controller script (or whatever you’re using on the player). The same thing applies here, perform a check before you run the function to move the platform.

Depending on how many platforms you have, a platform manager would probably be best. Although, admittedly, I tend to make a manager for anything that happens more than twice.

what exactly do you mean with isRunning condition? Can’t find anything related on the script reference, so I guess you mean some kind of flag that I have to manage, but based on what?

I was just about to post a question about how to approach collisions with a 2d game. I’ve been playing around with rigidbody based and raycast based. I haven’t tested OnCollision with 2 objects - does the event get triggered twice? Are you overriding the physics calculation and just setting the velocities manually?

And TBYG mentioned moving platforms that I can’t see in your original post, but it’s an interesting issue. I’d think you could do 1 of 2 things: Either do a Vector3.Distance to the platform in your OnCollision and remember it. If another platform hits you with an OnCollision event and the distance is closer, you replace the reference with the new one. Keep the player’s self-set velocity in one group of variables, and set the physics velocity to be the player’s velocity + the platform’s velocity.

That’s if my idea of using physics velocity works. I guess you can essentially do the same thing with physics movement but I guess the problem there is only calling it once.

For some reason I was thinking moving platforms, I apologize. However, the same can be applied here.

isRunning would be a Boolean, a check. If the player is in contact with a platform, enable the check. If not, turn it off.

The other option would be a raycast. Perform a raycast and set all the platforms to one layer.

I guess we would née a better description of what the issue is. What are you trying to accomplish by having your player trigger the platforms?

I’ve got a question about raycast - what is the thickness of the ray? In other words, if I have a sprite and I do a raycast directly down from the center of the sprite and there’s a platform overlapping the sprite by 25% of its width, then will the ray miss it, or is the ray the full width of my sprite?

If you’re worried about the collides touching, just adjust the collider size while leaving the game object scale the same. I’m still having a hard time understanding what the exact issue you’re having is.

The platforms are not moving, and I manage the logic with a enum of states. That is not where the problem is, since when u collide with a single platform it works perfectly. My problem comes when I got the player in contact with two different platforms, which for some reason makes the OnCollision events go crazy, being called all the time (OnCollisionEnter and OnCollisionExit).
More information about my project: I am managing 2d Sprites with ex2d asset. This should be transparent to collisions, since it uses the unity’s collision manager. The player is a exSpriteAnimationObject, which has a box collider and a rigid body (kinematic) attached to him. I don’t use any of the default physics, I do it all manually. The script attached to the player is where the Collision events are programmed.
The platforms are exSpriteObjects, with a rigid body and the position and rotations fixed, and for now I just consider static platforms, for the sake of simplicity (I’m prototyping after all)
I hope this gives you more information about my problem.

bump

I’m doing some similar stuff and unless you’re fully utilizing the physics engine, using rigidbodies and its collisions isn’t the way to go. I’m not even really sure it’s possible as I think the 0 z-depth of our characters doesn’t work well with the physics engine collisions. Instead I think a combination of raycasting and Collision.Bounds checking is the way to do it.

In my testing I get perfectly consistent results using that technique. In my test I have a character that starts in the air and drops onto a platform. every Update() I’m moving him .1 to the right. He lands on the platform and slides along to the right perfectly, then drops off. The trick with raycasting is that it only compares a ray against a collision mesh, not the two meshes so you’ll need to set up several ray points to get your character to stay on the platform for the full width that you want them to be able to.