- Home /
How to set boundries for an object and reset its position?,How to change the position of an object when out of bounds?
I am creating a pong-esque game with physics using the "dynamic rigidbody 2d feature", however when i try setting boundries for goals the ball just starts off by rotation around the 0,0,0 point forever.
void Update() {
//Out of bounds code (too far left or too far right)
if (transform.position.x < -25f)
{
transform.position = Vector2.zero;
}
if (transform.position.x < 25f)
{
transform.position = Vector2.zero;
}
},
I have the following code to restrict the movement of an object when it enters a "goal on either side. However when I start the game the object (dynamic) just goes in circles around position 0,0,0.
void Update() {
//Out of bounds code (too far left or too far right)
if (transform.position.x < -25f)
{
transform.position = Vector2.zero;
}
if (transform.position.x < 25f)
{
transform.position = Vector2.zero;
}
}
Comment
Answer by _samuel12345 · Jun 08, 2018 at 08:21 PM
Shouldn't it be
if (transform.position.x > 25f)
instead of
if (transform.position.x < 25f)