Moving some UI images

Hey guys!

I have some UI images that I move around on the screen during gameplay and am having some pilot errors with the right portion of the screen.

Basically I have stuff going on on the left side of the screen and I know all the way left is 0, so the UI elements on the left of the screen are fine. But I am having issues on the right side of the screen, because the right side of the screen changes based on the size of the window.

I am trying to use Screen.width in my If conditionals but it is still behaving odd.

Basically I have a shape that I want to appear of the right side of the screen at a certain time, and I want it to float left and right within certain parameters. I will show you what I did for the left side and this works great:

if(transform.position.x >= 65)
{
moveLeft = true;
}

if(moveLeft)
{
moveRight = false;
transform.position.x -= speed * Time.deltaTime;
}

if(transform.position.x <= 35)
{
moveRight = true;
}

if(moveRight)
{
moveLeft = false;
transform.position.x += speed * Time.deltaTime;
}

I know I hate using hard values, but since the left side of the screen is always 0 (I am assuming from my extensive prior 2d experience) this solution works wonderfully for the left side of the screen. I have the exact same UI element on the right side of the screen at the exact opposite position on the canvas, but I can’t seem to make it work.

Would anyone be willing to help me rewrite this block of code for the right side of the screen? It would be greatly appreciated!

I was able to figure it out, using the screen width and creating a local variable that grabs the starting position then checks it against the current position. :smile: