How to make an object glide to one position, and then another

So, I would like an object to move one direction, and then instantly move in the opposite direction. Like, this.position.x += 30. one it reaches 30 on the x-axis, for it to then instantly right after to run this.position.x -= 30, until it reaches zero again, and then for it to stop. I am not sure how to do this.

You can either design an animation in a modeller… or you can use a Tweening library like TWEENJS or GSAP to animated the position.

and to supplement @manthrax aswer, here is an example of tween chaining

1 Like

The easiest way I would go is MathUtils.pingpong()

1 Like

Pingpong cool.

I couldn’t find an example of using pingpong anywhere, so I created one.

3 Likes

I used it here, but in shaders (ported from MathUtils, line 195 in the JS section) :slight_smile:


Demo: https://codepen.io/prisoner849/full/NWoZgGV

5 Likes