- Home /
Moving Trigger/Platform and Moving Player calls OnTriggerEnter every frame
Hello!
I have a moving platform with a rigidbody attached and my player character uses a CharacterController and also has a rigidybody attached. All rigidbodies are kinematic.
Hence, when I want the player to stand on the platform and kep above it as it moves, I have a trigger above the platform, so when the player triggers it, he is nested as a platform's child, hence being moved when the platform moves. Here is the piece of code to achieve so:
void OnTriggerEnter(Collider t)
{
Debug.Log("On trigger enter in " + gameObject.name);
if (t.gameObject.tag == "Player")
{
t.gameObject.transform.parent = gameObject.transform;
Debug.Log("Parented");
}
}
void OnTriggerExit(Collider t)
{
Debug.Log("On trigger exit in " + gameObject.name);
if (t.gameObject.tag == "Player")
{
t.gameObject.transform.parent = null;
Debug.Log("Unparented");
}
}
However, as the platform moves, the player "sinks", slowly decreasing its y-value until it falls. I also noticed that as the platform moves, OnTriggerEnter is called on every frame! Also, no OnTriggerStay is called, even though the player collider is ALWAYS touching the same trigger. Furthermore, OnTriggerExit is NEVER called, I have to manually dettach the player when I see he is not touching the ground anymore, which is really NOT good.
Here is a sample of the log:
Any idea on what is going wrong here, on how I can improve it? I would really like to stick to OnTriggerEnter, avoiding OnCollisionEnter whatsoever.
By the way, I am moving the platform with transform.Translate and the player with CharacterController.Move.
Thanks in advance!