1
\$\begingroup\$

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.

enter image description here

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.

enter image description here

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.

\$\endgroup\$
2
  • \$\begingroup\$ Why not just use two colliders for the platform (divided horizontally) and only let the object pass through the top one if it has also passed through the bottom one? \$\endgroup\$ Commented Nov 29, 2017 at 11:19
  • 1
    \$\begingroup\$ We have tried this as well earlier. It's potentially the same as our second solution with trigger colliders from all sides of the static object. It's prone to bugs and harder to maintain when there are multiple static objects available - to make it clear, these objects are static only in the demo for this functionality, they will be moving from right to left and vice versa when fully implemented. \$\endgroup\$ Commented Nov 29, 2017 at 11:25

2 Answers 2

2
\$\begingroup\$

If you are doing a 2d game, Unity already has this functionality built in. It’s called a platform effector. You can set it to work only one way, so that way collision from the top works but not the bottom. You just have enable use one way on the platform effector 2d component.

If you are doing a 3d game you can use a non convex mesh Collider Using a plane as the mesh. The collision will only work one side using this technique as well.

\$\endgroup\$
2
  • \$\begingroup\$ We will check this out today. Thanks for the info! \$\endgroup\$ Commented Nov 30, 2017 at 8:43
  • \$\begingroup\$ This worked as a charm, thank you! I will post a more detailed explanation as the answer to this ticket. \$\endgroup\$ Commented Dec 1, 2017 at 8:17
0
\$\begingroup\$

Assuming I'm understanding the intended behaviour correctly, you could simply compare the distance of, say, the raycast origin or its hit.point (both should work) to

  1. the black cube's position and
  2. its position offset by the normal of whatever direction you want to check.

So for example:

Vector3 middle = blackCube.transform.position;
Vector3 shifted = blackCube.transform.position + normal * 0.01f; //some small value

float x = (middle - origin).sqrMagnitude - (shifted - origin).sqrMagnitude;

if (x <= 0) {
  //handle one direction
}
else {
  //handle other direction
}

PS: The reason I'm using Vector3.sqrMagnitude here is that it's faster than Vector3.magnitude (https://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html)

\$\endgroup\$
1
  • \$\begingroup\$ We will check this solution as well today, thank you for the response! \$\endgroup\$ Commented Nov 30, 2017 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .