How to move part from start position to end position in perfect zig zag pattern

Hi there! Basically, what I wanna do is calculate a perfect zig zag pattern movement for my projectile part from start position to end position. By the way when I say perfect I mean I want it to go left and right/right and left everytime without breaking pattern and the left right/right left path lengths to stay the same.

Examples:
Ignore the curve path!
image

I have tried looking on Devforum, google, and other lua scripting help websites like scripting helpers but I can’t find anything to help me.

Thank you for looking at this question/topic!

maybe you could use math.sin(tick()*speed) and if you want it to perfectly zig zag into a specific position you could stop the loop after the magnitude between the projectile and the target position is a certain amount

2 Likes

May you go more into detail on what math.sin would do and how to implement this into a projectile?

math.sin is a sin movement looks like this


you could add it into example cframes by looping

Part.CFrame *= CFrame.new(0,0,part.CFrame.RightVector*math.sin(tick()))

or doing tick()*speed if u think tick is too slow
it often used in camera shake but can also be used in part movement, i like to use it for cframe animations

2 Likes

Is it a sharp vertex when it is zig zagging? If it’s not then I’ll still test to see if I like it.

the transition between the zig zags are pretty smooth in my opinion probably depends on the speed but here is an example of a sin looped animation i made


also i might have wrote the code wrong if it doesnt work add

CFrame.LookVector*amount u want to move*math.sin(tick())

this one uses 1*math.sin(tick()*4)

1 Like

What do I do if I want it to zig zag faster?

I’m assuming I would make the loop faster

you would multiplie the tick

CFrame.RightVector*5*math.sin(tick()*10) --multiplies the speed with 10
1 Like

This is very cool, thank you! I will try this out on Friday and see if it works and if it does I’ll make it as the solution. I’m very grateful for your help!

1 Like

if it doesnt work another method would be to create a table of segments by getting the magnitude between the target position and the origin position the dividing it so you get your desired amount of segments. then loop through it with ipairs and tween the projectile between those segments, almost like a lightning strike

1 Like

Alright thank you so much, this is very cool information too!

1 Like

Oh also can you show me how to do this just in case the sin method doesn’t work. Just a reminder I want it to have a perfect zig zag too. Thank you!

The sine method is pretty reliable if you use it well.
Although, if you want a sharper or longer zigzag, you can use modified expressions of sine. One such example is tan(sin(x)).

math.tan(math.sin(tick()))

Play around with these suggestions and if you encounter any problems feel free to ask questions.

1 Like

Omgosh thank you so much! This is very cool too!

is there a way to make it even sharper?

if not it’s fine! Thank you so much!