# How do I make a 2d sprite move horizontally like a marquee

How do I make a 2d sprite move horizontally continuously like a marquee, I have tried this numerous times with no results, I can’t figure out what I am doing wrong, please advise.

It won’t appear half and half, but you can use Viewport coordinates to make this happen. Viewport coordinates go from (0,0) in the lower left corner of the screen to (1,1) in the upper right. So any value out of the range 0 to 1 is outside the view of the screen. Of course you need to allow a bit extra for the size of the object since typically only the pivot point is checked. So to test on the right side:

``````var pos = Camera.main.WorldToViewportPoint(transform.position);
if (pos.x > 1.05) {
pos.x = -0.04);
pos.z = transform.position.z - Camera.main.transform.position.z;
transform.position = Camera.main.ViewportToWorldPoint(pos);
}
``````

The ‘1.05’ and the ‘-0.04’ will need to be adjusted for the size of your objects. If you are planning to do all four sides of the screen, you need to be carefully about how you reset the values so that you don’t trigger a re-swap. For example, if you were doing a left to right and a right to left swap, and you set the the left swap position too low, it would swap back on the next frame.

Here is a bit of example code pulled from a asteroids test project written awhile ago:

``````void Update ()
{

Vector3 v3Pos = transform.position;
v3Pos = Camera.main.WorldToViewportPoint(v3Pos);
if (v3Pos.x < -0.02f)
v3Pos.x = 1.02f;
if (v3Pos.x > 1.02f)
v3Pos.x = -0.02f;
if (v3Pos.y < -0.02f)
v3Pos.y = 1.02f;
if (v3Pos.y > 1.02f)
v3Pos.y = -0.02f;
v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);
v3Pos.z = 0.0f;
transform.position = v3Pos;

}
``````