Trigger - Enter/Exit seems to jump back and forth

I’m programming a platform game. Right now I’m working on a platform module that moves down when you jump on it, and back up when you jump off it.

The moving platform is a basic Cube with a Box collider. It has a child Cube that lies on top of it that acts like the trigger. The trigger box has a OnTriggerEnter and OnTriggerExit which controls how we move the platform.

Im using the character movment scripts from Unity 2D tutorial.

This all works fine except one thing:

When the platform moves down faster and faster, it gets to a point where I get a lot of hickups in the movement. Print-out from OnTriggerEnter and OnTriggerExit shows that they are triggered on-off-on-off-on-off-…etc

I think this has some thing to do with that the platform moves down using a formula, while the character moves down using “gravity” from the PlatformerController.js script of the 2D tutorial.

But the funny thing is that i certanly looks like the character is inside the trigger box all the time, even though OnTriggerExit says something else…

Making the trigger box bigger, or the speed of the moving platform slower, and these problems go away. But that a “bad hack”, I need to know why the OnTriggerExit is triggered when it really looks like it shouldnt?

None of the gameobjects involved contains a ridget body component.

Any thoughts on the subject?

There should be at least one rigidbody in order for the OnTrigger events are raised, AFAIK.

Maybe you should use some of the moving platforms physics and ideas found in this forum.

Also, you may want to check this link: Unity 2D tools for game dev – evolved for optimal graphics performance

It’s a 2d game that has some platforms. Might give you ideas.

I find it strange then that I actually do get trigger events, when my character and platform definitely doesnt have a rigidbody attached.

Yes, the 2D tutorial you mention (i also mentioned that) is the one im using as a base.

My character is cube with Character Controller and PlatformerController (script from 2d tut) attached.

My platform is two cubes, bottom being normal, top acting as trigger.

My code inside OnTriggerEnter and OnTriggerExit is executed, but i have the problem mentioned in my first post.

Very strange…

Is it possible that there’s an object (maybe hidden inside the hierarchy) that actually has a rigidbody? and that’s the object you’re “colliding” against?

Pretty sure, Im looking at them now:

Character:

  • Cube (Mesh Filter)
  • Mesh Renderer
  • Character Controller
  • Platformer Controller (Script)

Platform:

  • Cube (Mesh Filter)
  • Box Collider
  • Mesh Renderer

Trigger: (is a child of Platform above)

  • Cube (Mesh Filter)
  • Box Collider (Is Trigger checked)
  • Platform Mover (Script)

Thats it! The “Platform Mover” script has the trigger functions, and they are called.

The trigger is positioned right above the platform. It seems like the size of my trigger box has something to do with this, cause my problem only happens if the platform moves too fast and the box is not big enough.

Im blank and frusterated, thanks for any help :wink:

I know this is old, but for anyone else coming to this page: The Character Controller acts as a rigidbody!