True Asteroids-like Wrapping

Hey all. Currently working on a little project that is a clone of the Asteroids game using my own assets and code. Not really sure how to attach the project file or else I would.

My issue is with the iconic Asteroids screen wrapping. So far, I’ve gotten this to work on the top, bottom, left, and right by having various box colliders which teleport the player when the collider is triggered.

 private void OnTriggerEnter2D(Collider2D collision)
    {
        if (gameObject.CompareTag("LoopBoundTop"))
        {
            if (collision.gameObject.CompareTag("Player"))
            {
                collision.transform.position = new Vector3(collision.transform.position.x, -5.5f, collision.transform.position.y);
            }
        }
}

The result is this:

The issue comes when the players ship has a rotation. Instead of truly wrapping around the screen like I want, the code simply teleports the player down. Here’s what I mean:

What I want to happen is the red. What actually happens is the green.

This isn’t a bug in my code, I’m just not really sure how to implement what I want. One idea I had is to get the rotation of the player and subtract it from the teleport value, so that the greater the player’s rotation, the more it would “mirror” the player when they are teleported. I’m not really sure how to implement it.

Any thoughts or ideas? Would appreciate it.

This isn’t a bug in your code? Whose code is it a bug in?

collision.transform.position = new Vector3(collision.transform.position.x, -5.5f, collision.transform.position.y);
            }

Ignoring the fact that if it moves in physics, it should be a Rigidbody2D moving and not you modifying Transform directly, this code doesn’t make sense: Vector3 where X position is collision X, Y position is -5.5 and Z is collision Y.

Personally I’d use raycast in the direction of motion (Rigidbody2D.velocity) to see where you hit the edge (the collider). If this distance is within a fixed (but configurable) radius of the player then consider you hitting the edge. You now know the point you hit on one edge. With that point (or slightly away from it) you can raycast the opposite way which will give you the point in the opposite direction on the adjacent edge (it also might give you another edge) and start the player there. These edges of course should be off screen by at least the player radius. None of this requires triggers and I’d simply add four EdgeCollider2D for top, bottom, left & right.

In reality, I’d not use physics because this can be done with 4 simple Plane for top, bottom, left & right. You can even do Raycast and GetSide or just define this rectangle with a Rect and use that but you’d need to project the backwards movement yourself.

Seems we passed each other. I updated my post with some ideas. If you’re sticking to using physics then I’d go with the raycast route as it determines both where you hit an edge and also the method to back project it to an opposite edge which gives you a re-emergence point.