- Home /
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.
Answer by Quokmoon · Mar 11, 2014 at 04:36 AM
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;
So I tried robert's solution first, didn't notice he answered. So I tried Quokmoon's solution then.
Here's what happens the user presses Up arrow and we get gui.upClicked == True, hence moving WHILE the button is pressed but it went insane on me. I click up arrow and switches between maxY and $$anonymous$$Y....What is it Im doing wrong? The idea is that when the player reaches 16 it stops moving....
Vector3 tempPosition;
private float $$anonymous$$Y = -16f;
private float maxY = 16f;
private float $$anonymous$$X = -32f;
private float maxX = 32f;
public void UpDownLeftRight()
{
if (gui.upClicked == true)
{
//$$anonymous$$ove Zombie
transform.Translate (Vector3.up * speed * Time.deltaTime);
/*transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$X, maxX);
transform.position.y = $$anonymous$$athf.Clamp(transform.position.y, $$anonymous$$Y, maxY); */
tempPosition = transform.position;
tempPosition.y = $$anonymous$$athf.Clamp(tempPosition.y, maxY, $$anonymous$$Y);
transform.position = tempPosition;
gui.upClicked = false;
}
}
nvm maxY and $$anonymous$$inY were mixed up hahaha, thanks for your time guys!
Answer by robertbu · Mar 11, 2014 at 04:10 AM
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);
@$$anonymous$$ii - If you are coding in C#, the look at Quokmoon's answer. $$anonymous$$y answer will only work in Javascript since in assigns individual components of transform.position. In C# you have to update the whole position.