This is a simple game where player jumps platforms.
Here in the above image, the Player needs to jump on to the moving platform and move with it. Then later it has to jump back on to stationary platform and not move with moving platform.
Problem :- When the player jump back from Moving Platform to stationary platform the OnTriggerExit doesn’t activates immediately, Moving platform kept pulling the player till it falls down. A while later, OnTriggerExit activates and player moved out from being Moving platform child.
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
print("Yes, Player collided with moving platform");
other.transform.parent = this.transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
print("Yes, Player exit moving platform");
other.transform.parent = null;
}
}
Which collider is the trigger? If your character is landing on the platform, then I assume the platform’s collider is not a Trigger collider. Are you sure the trigger collider has the same volume as the platform itself? And how much of a delay are we talking about?
Here is the inspector images below.
The Moving Platform has two colliders with one ‘isTrigger = true;’
Player has a Capsule collider and Character controller.
The trigger collider on Moving Platform has a 0.15 offset in Y from center initiate trigger a little early.
Nothing major stands out. You didn’t say how much the delay was on the trigger events firing. In general, triggers don’t fire on the very first frame that an overlap occurs. The Physics system will cause objects to overlap on one frame, and then only fire TriggerEnter/Exit the frame after that. But that should be nearly the same instant, unless you’re doing some very special stuff.
The only thing to mention is you probably should be using Rigidbody.MovePosition to move the platforms, rather than setting the transform position directly.
Sorry, about the delay. The player once jump back to stationary platform gets pulled off by the Moving Platform. Whenever the player falls down to the space (off the screen), it is removed from the Moving Platform’s child, also the print happens at that time. Ideally, the moment the player jumps off the Moving Platform TriggerExit must trigger, right?
I am NOT using Rigidbody.MovePosition. I am using CharacterController.Move in Player script.
Note :- Yes, When I disable CharacterController component TriggerExit works, but Player won’t move.
Now, when I add the parent=null code within TriggerEnter method in Player script as below, it works well as desired (Not an ideal solution). I am confused why TriggerExit doesn’t work.
private void OnTriggerEnter(Collider other)
{
if (other.tag == "StationaryPlatform")
{
print("Yes, Player exit moving platform");
gameObject.transform.parent = null;
}
}
I meant that your MovingPlatform script should probably be using MovePosition instead of setting the transform manually:
I try to avoid parenting objects to moving platforms, as that seems like it can introduce some weird behavior at times. I realize it seems like a common thing people do, though. But I haven’t had good luck with it. I’d probably recommend just adding Debug.Break() and other diagnostics to pinpoint exactly when these events are actually firing, and pause the editor to observe that.
I tried debugging and pause, I found few things which can be the potential issue. Not clear.
Player has Capsule collider and Character controller which both of these are colliders. Once Character controller disabled, the player reacts as expected, TriggerExit works. (But inputs won’t work)
The Moving Platform has two colliders, one for hard surface collision and one for collision detection. When I increased collider for collision detection offset distance from 0.15 to 0.5, TriggerExit works properly.