We are trying to determine which side of the collider is hit in order to let the colliding object pass through or land on the other object.
Our previous failed attempts included detecting object velocity and changes on Y axis in our 3D game with locked Z axis (simulated 2D).
Currently, the detection is working by calculating the velocity - when the "Object in motion" hits the top of the black cube ("trigger collider") by the certain level of velocity (determined by gravity) the "collider" is activated. Otherwise, when the velocity is higher - the "object in motion" passes through. This, of course introduced a lot of issues and the solution itself is not elegant.
In the other scenario, we have tried adding trigger colliders around the "static object". This also complicates the process when "object in motion" has an arc when landing. Likewise, this solution is hard to maintain and prone to bugs.
On top of that, we implemented Raycasting. But we are not getting consistant "Top" and "Bottom" trigger indications when using it. Here is the gist of that code.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("my_cube"))
{
var hit = HitDirection();
Debug.Log(hit);
}
}
bool IsRightOrLeftHit()
{
RaycastHit hit;
Ray rayUp = new Ray(transform.position, Vector3.up);
Ray rayDown = new Ray(transform.position, Vector3.down);
Physics.Raycast(rayUp, out hit);
Physics.Raycast(rayDown, out hit);
return hit;
}
We would appreciate any hint or help, and please ask me any questions if the explanations are not clear enough.