# How to "Change Lanes" with Keys?

I have a sphere falling endlessly. It is locked on a 2D plane. I would like to, with the A & D keys, move the sphere back and forth between lanes. I don’t want to freely move back and forth. I want the sphere to move a set distance left or right and always end up in the middle of a lane.

The sphere should always take the same amount of time to transition between lanes regardless of its falling speed.
Thank you for any help, advice, or tips. Below are two simple mock-ups of the situation.

Hello @ToddRuggley,

What do you mean when you say, “I don’t want to freely move back and forth. I want the sphere to move a set distance left or right and always end up in the middle of a lane.” Does this mean that you always want the camera to focus on the sphere so that it is in the middle of the screen?

Farshooter

Farshooter,

Thanks for the reply. What I mean is when I Press A(left) or D(right) I want the sphere to move a specific distant left or right. Holding A does not make the sphere move further left. Just one and done. That set distance will always put the sphere in the middle of whatever lane you’re moving to.

If I am in Lane 1 and I want the go to Lane 3 I have to Press D twice to get there; not just hold D down.

Please if I’m still not wording this is the correct way let me know as I am more than happy to try again.

Thanks again

Hello @ToddRuggley,

I now have enough information from your comments to know what you are trying to create.

Important Side Note:
My techniques are assuming that all the lanes are of equal width. These techniques will not work if the lanes have varying width.

I answered a similar question in this post so you might want to check that out as well. Essentially what it boils down to is that there are two ways to do this, both of which have their advantages and disadvantages.

Method 1:
Add a physics impulse in the direction you want to move and have the force to some fixed value so that you move the same distance every time. This results in smooth movement and you don’t have to worry about your object teleporting through other objects. A major disadvantage to this technique is that there is no way to set the exact distance that your object will move.

Method 2:
Directly manipulate the actors location. In other words directly set the actors location to a set position in either direction. This solution gives you total control over exactly how far the object will move. A disadvantage to this method is that it will look jerky. To fix this you could interpolate the movement using interpolation techniques. Another disadvantage to this method is that the object will move through objects on either side. You can solve this by using traces to check whether or not an object is in the way of where you want to move.

For your situation I would recommend Method 2 solely because you can set exactly how far your object will move. This will keep the object in the center of the lanes. I would also use some method of interpolation to give the object smooth movement. Also if you object is not going to collide with anything then you obviously won’t have to worry about collision.