How can I mirror (and rotate) an object through a line as were it a portal?


6 days ago


20 time


I want to create a 2D portal effect. Thus, the object exiting the exit portal should correspond in location/rotation with the object going through the entry portal. This until half of the original object is through the portal.

I have made a sprite cube that should be mirrored over a line (the portal). The cube exiting the portal is just a clone of the original. If we rotate the portal, the 'reflection' cube should rotate along with the location of the original cube compared to the portal. If we rotate the original cube itself, the 'reflection' should be mirrored as well with regard to the portal. I just can't figure out which rotations to do and in which space. I have tried many things with local transforms etc.

I use a Vector2.Reflect to get the correct position for the reflection:

  (line.transform.position - cube.transform.position), line.transform.up

Image 1

To mirror the cube as a reflection, I add this rotation:

reflection.transform.localRotation = Quaternion.Euler(0, 180, 0)
  * cube.transform.localRotation;

But now if I rotate the portal, the reflection should rotate the same way as the original cube rotates in the space of the portal, like drawn poorly here:

Image 2

Does anyone know which are the correct transformations / rotations to accomplish this, like the localRotation / localTransform calculations? I've tried so much but somewhere along the line my understanding of the correct math gets lost.

Sorry, can't post images yet :)

1 answers


I'd suggest simply reflecting cube.transform.forward:

reflection.transform.forward = Vector2.Reflect(cube.transform.forward, line.transform.up);