Stop the player when striking the edge of the screen

Hi guys!

I’m having a little trouble to make the player stop when attain being the display limit. I used the code:

if (transform.position.y > 14)
            transform.position = new Vector3 (transform.position.y, 14);

But when the player touches the edge of the screen it moves it to as it should, but teleports 10 x too, what is the reason?

thx!

The parameters of Vector3() are x, y or x, y, and z.

Using physics? Just put up an invisible collider

1 Like
Vector3 pos = transform.position;
if (pos.y > 14f) {
    pos.y = 14f;
    transform.position = pos;
}
1 Like

I’m not using psychic or rigidbody and thanks Ben, work!

But, i added more line to catch the bottom edge too, like this:

Vector3 pos = transform.position;
        if (pos.y > 14f)
        {
            pos.y = 14f;
            transform.position = pos;
        }
        if (pos.y > -15f)
        {
            pos.y = -15f;
            transform.position = pos;
        }

But when I start the game, the player teleport y -14. I’ve removed the pos 14 and only left the -15, but this only occurs when a negative value is added. I’m looking for the causes. Thx!

Is your player parented to something?

The > -15 should probably be < -15

2 Likes

No.

Oh… is really this, thank you XD