I have seen quite a few posts, one in particular that I can’t find right now and why I am posting it here, have asked how to wrap movement from one side of the screen to another…
well I am sure this is known by some but say the extents of your screen on the x-axis is 9.0 and -9.0 in a simple 2D, Asteroids type game. All you need to do is something like:
In the process of making a Breakout clone and accidentally switched the positive and negative numbers in the Clamp function and it gave me instant wrapping.
Hopefully the guy who was making the Asteroids game will see this as I can’t seem to find the post right now.
Thx! I havent post the original thread but this has help me. But, one thing, anyone knows how to make the screen warp smoother so the object starts going out form one side of the screen an starts appearing in the other side (so you have half of the object visible in one side and half in the other).
@Eyeofgod:
That’s tricky and can only be done when you have your object/character two times an the scene.
My approach would be to have your object 2 times in your scene seperated by the whole screensize.
That way you can see the object on both sides when it enters or leaves the screen.
I would do something like:
// The actual player
void Update()
{
// NOTE: this works only if the cam center is at 0,0
Vector3 pos = transform.position;
//*** apply your movement here ***
// change pos as you want
// this is the half size of the screen (so it goes from -9.0f to 9.0f)
float HalfScreenSize = 9.0f;
// wrap check
pos.x = Mathf.Repeat(pos.x+HalfScreenSize,HalfScreenSize*2)-HalfScreenSize;
transform.position = pos;
}
and on the second instance this:
public Transform OriginalObject;
void LateUpdate()
{
// NOTE: this works only if the cam center is at 0,0
// this is the half size of the screen (so it goes from -9.0f to 9.0f)
float HalfScreenSize = 9.0f;
// Get the actual position of the original
Vector3 pos = OriginalObject.position;
// Add the screen size
pos.x += HalfScreenSize*2;
// wrap around if necessary
if (pos.x > HalfScreenSize*2)
pos.x -= HalfScreenSize*4;
transform.position = pos;
}
Notice the LastUpdate function! otherwise the second instance would be 1 frame to late.
It looks quite complicated but that’s because the cam center is in the middle.
It would be much easier to have 0 on the left side.
That’s the case when your scene origin is on the left side of the screen
// The actual player
void Update()
{
Vector3 pos = transform.position;
//*** apply your movement here ***
// change pos as you want you want
// this is the half size of the screen (so it goes from 0.0f to 18.0f)
float HalfScreenSize = 9.0f;
pos.x = Mathf.Repeat(pos.x ,HalfScreenSize*2);
transform.position = pos;
}
public Transform OriginalObject;
void LateUpdate()
{
// this is the half size of the screen (so it goes from 0.0f to 18.0f)
float HalfScreenSize = 9.0f;
Vector3 pos = OriginalObject.position;
pos.x += HalfScreenSize*2;
// here we need now 3 because everything is shifted to the left
if (pos.x > HalfScreenSize*3)
pos.x -= HalfScreenSize*4;
transform.position = pos;
}
ps. that’s C# - code … sorry but i don’t like to use “dirty”-languages
@Bunny83 , as you said, “the actual “size” of the screen in world units depends on your orthographic size of your camera and the aspect ratio.” How can we make the object wrapping ortho size and aspect ratio independent so that it can work for any screen size?
i’ve implemented the second method here into my program (scene origin on left) with the player and another duplicate player, i’m facing an issue where when the player is on the first left half of the screen it’s actually displaying the duplicate entity, which doesn’t properly collide with other objects on the screen. any way to fix this so it’s always the real player on the screen?