- Home /
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.
It would also work if I could somehow get the direction of the current speed. then I could take the x component and evaluate if it's positive.
Answer by The_r0nin · Feb 23, 2011 at 05:16 PM
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.
Answer by cregox · Feb 23, 2011 at 05:30 PM
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;
}
Your answer
Follow this Question
Related Questions
Grid-based movement: How to face forward 2 Answers
Why does my character only dash forward and not in the moving direction? 1 Answer
how to make my character walk automatically and when pressing a button change the direction 2 Answers
Character facing wrong direction when moving vertically 0 Answers