How to make object move left and right smoothly

Hello, im trying to create a moving background.
It should accelerate left, slowdown, then accelerate right, slowdown and repeat.

I have been able to make the background move left, then slowdown, then accelerate left once more.
I have been trying to apply a force on void start so the initial velocity will be countered, and then create a looping motion, but to no avail. 200039-screenshot-2022-09-21-130607.png

Here is the code, where the object will move right, slow down and then accelerate right again.

Does anyone know how I can make it smoothly move left and then right?

Thanks a lot in advance.

Greetings @jegersej

This is not a quick answer but I’d use Animation. You’ll need to find some tutorials to see how to make it work but the advantages are that you can then set Animation curves that ease in and ease out to help set your required acceleration.

The basis is that you animate a transform position starting at one value and migrating to another value over a certain period of time. You add the animation to a game object (in your case the background) and , when you run the game, the animation moves for you without needing any script. Definitely worth finding a video tutorial for…

take out direction = direction * and -1

then

movement = new Vector2(direction, 0); timer = maxTimer; if (direction == 1) { direction = -1; } else { direction = 1; }