# Determine global movement of gameObject

I have a ball bouncing around in a ponglike game. The ball only bounces in the y, x plane.The ball is supposed to bounce from left to right to left, etc, so to avoid it from getting stuck bouncing only up and down I applied a constant force.

This constant force either pushes it to the left or the the right.

Now I need a way to determine is the ball's current movement in the x-plane is positive or negative. When it's negative (moving left) the constant force needs to be negative (push ball more to the left) and if it's positive vise versa. I have no idea how to determine the movement of an object in the global x axis though. Many thanks in advance.

Record the X position as the very last thing in your movement loop. Then compare your X position in the next frame with the position in the last frame. If the difference between them is positive, you are moving in a positive direction. If negative, the same.

Depends on how you're making it bounce. If you're using rigidbodies / physics, just grab its velocity vector x. If you're implementing your own, you have to grab the delta of previous position and current so you know its current velocity. Then you grab the X component from that as well. Here's an example:

``````var oldPos : Vector3;
function Update () {
var delta = transform.position - oldPos;
print(delta.x); // it's moving one side if it's negative or positive to the other.
oldPos = transform.position;
}

``````