2D Background Stop Moving on BoundingBox Exit?

Hello everyone, I currently have an Object that’s moving around a background(which is a sprite.) This is how the object moves.

The idea of movement is Gameboy Zelda style. I’ve created a small HUD with arrows.

(the movement script is attached to the player…)

If up arrow is clicked

transform.Translate (Vector3.up * speed * Time.deltaTime);

If down arrow is clicked

transform.Translate (Vector3.down * speed * Time.deltaTime);

If left arrow is clicked

transform.Translate (Vector3.left * speed * Time.deltaTime);

and so on…

Now I want to accomplish :

  • Don’t move the player if the limits of the bounding box of the Background
    sprite are reached.

I haven’t started to do any scripts because I honestly am blank on how to start tackling this problem thats why I come to you for help.

If you think I should change the way the object moves please let me know why, and what to modify it to.

Thank you for your time and hope hearing for your answers.

Since this is a 2D game, the easiest solution is to clamp the position. Move your character to the maximum limits x and y and record the positions. Then just below your transform.Translate()s above, do:

transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);

1.You can put validation bound using Mathf.Clamp

Example :suppose If you pressed up arrow
Object transform should be:

Vector3 pos = target.position;
pos.y = Mathf.Clamp(pos.y, 2.0f, -2.0f);
target.position = pos;