The Square! It stays on the screen!

No more will my objects fall off the world screen! Good god, I love the internet, it really makes my life easier.

So, what I was missing was the Camera.main.ViewportToWorldPoint function. It can be used to pull the edges of the screen, so that even if I switch my resolution, the borders will stay true for that resolution. So, here it is: A bordered world for my little white square


var leftBorder = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;

if (Input.GetKey(KeyCode.LeftArrow))
if (transform.position.x > leftBorder)
transform.position += Vector3.left * speed * Time.deltaTime;

Now, there is a little problem… If my speed is too high, lets say 20 (its 5 now), it’s possible that my little square can jump 19 pixels off  the screen if the center is only 1 pixel away from the border.

So, next step? Make it so that if it would disappear, force it at the the edge. How to do this? I have a couple ideas. I’ll be back later on that!


2 thoughts on “The Square! It stays on the screen!”

  1. I wonder if I could make that work. As for more posts, I’ve got one almost ready, but I got busy with some AutoCad work for the thesis. I’ll make a post tonight, with a working version of the “game”.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s