A
A
Aizharik2018-09-03 20:21:12
C++ / C#
Aizharik, 2018-09-03 20:21:12

How to move a character to the back side of the screen if he went out of the camera view on the other side in Unity (2D)?

I'm not strong in C#, I'm trying to learn Unity, using the example of creating a simple 2D game.
Tell me how you can implement to move the object (character), for example:
If the character goes beyond the visibility area on the left side, then it appears on the right side of the screen and vice versa. With the preservation of speed and direction of movement. For some reason, I think in Unity this is done simply, Google didn’t help, maybe I’m not asking correctly :)
So far, I’ve made empty objects with a collider on the sides, upon collision with which I return the Persian to the point of appearance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2018-09-03
@aizhar777

Well, the option with colliders is nothing like that.
In fact, there is no magic bullet. Variants with checking screen coordinates (Camera.main.WorldToScreen... and then check if you go beyond the screen - teleport) when moving come to mind.
Preservation of speed and direction - so it does not change when teleporting - when teleporting, you only change the coordinates. Although depending on how the speed and direction are set (in theory, there should be a vector relative to the center of the object).
It is necessary to return not to the point of appearance, but to read the new coordinates relative to the exit point for the screen. For example, the object went off the screen on the left. So its x-coordinate is less than zero (adjusted for size - xs where you have the center). Accordingly, we make x equal to the length of the screen, adjusted for the size of the object, and we translate all this into world ones. In the forehead, the solution looks something like this.
If you need an object to be "ass on the left, muzzle on the right" - i.e. half states are more complicated here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question