**Scenario:**
- 1 Platform GameObject (contains 2 Box colliders - 1 trigger/other not)
- 1 Cube GabeObject (contains box collider and player controller)
- The platform moves up and down
- The cube rides the platform
The cube has a script that applies gravity and allows the cube to jump. The cube has Trigger events that handle parenting the platform.
(See code below)
Expected Result:
- Cube lands on platform
- Cube becomes child to platform
- Cube moves with platform
- Cube jumps
- Cube no longer child to platform
- Cube falls
- Repeat
Problem:
The issue occurs when the platform moves downward. This causes a TriggerExit() to be thrown and the cube receives this before moving with the platform. This causes a bouncing issue while the platform moves down.
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxGravity : MonoBehaviour {
public float jumpForce = 20f;
public float gravityScale = 10f;
private CharacterController controller;
private Vector3 moveDirection;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Platform")
{
transform.parent = other.transform;
}
}
void OnTriggerStay(Collider other){/*Same as enter*/}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Platform")
{
transform.parent = null;
}
}
void Start () {
controller = GetComponent<CharacterController>();
moveDirection = new Vector3(0, 0, 0);
}
void Update () {
if(controller.isGrounded)
{
moveDirection.y = 0;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection*Time.deltaTime);
}
}
Sample image:
Question:
How can I prevent the TriggerExit() from getting called before the child moves with its parent?
If not possible, how can I prevent this bouncing issue?

I have tried raycasting, but this just comes with too many issues. Its costly to raycast, especially if done constantly. This also causes landing and jumping issues. If the raycast is too short, then the issue doesn't get fixed and more resources get spent, if its too long then jumping becomes too complicated and normally does not work. This can also cause landing problems and creates a landing that is not smooth. I'm looking for some way I can prevent an improper call to
– uulamock_unityOnTriggerExit()or some kind of check or flag to determine if an improper call was made.