# How to get a zig zag movement between two points?

Sometimes I don’t want my AI to walk straight because it’s too easy to kill.
I think it might be better if it take a zig zag way.

Something like that:

I am trying to do it as shown in the following image but it is turning out to be a complete disaster.

Any idea how to do it well?

Thank you very much!!

The code you made will give you a random direction every frame, which will make your character look like it bugged. How I would solve this is use lerps or timelines to smoothly change the characters movement direction over time and use sine node to flip-flop between two vector directions. This would create the movement in the image. You can also change the speed and range randomly, so it is less predictable if you want that.

2 Likes

Hello

Unfortunately something is wrong with lerp… It is supposed to return an intermediate point between A and B.

However “AI move to” function is failing.

It is possible that this point is not navigable or something like that… But I know that between my AI and my Player there are navigable points…

So I don’t know what’s going on.

i am pretty inexperienced with AI, but I was under the impression that you’d need to find navigable points along the way, and one by one move to them, like waypoints.

So it would be more like, calculate final destination, then get an array of navigable points between current location and final destination, then move to each and on move complete, move to next location in the array.

there is many ways to get zig zag points between here and there - i’m not a strong math guy so my ways are kinda hacky and stupid. somebody else might offer better ideas about that. But the idea is that you make a list of waypoints first, then move.

I am not sure that you can “steer” an ai along its way as it seeks a point. It wants to go the shortest route possible - assuming i understand the system correct;y.

2 Likes

Hi
Thanks for your idea. I’m going to study that!

I did something similar once with a custom tree task, that would remember whether the last move was a “zig” or a “zag” and then set it’s next destination to a direction left or right of where it was currently at.

BIGTIMEMASTER’s idea would probably also work.

Probably depends on what you need to do while moving

1 Like

Alpha for lerp should be between 0 and 1, but I would also do the setup a bit differently. You can generate path points with find path to location node and then use path points vector array to move from point to point and then use a method I mentioned above to make character move left/right. To move from point to point you can use Add movement input and do some vector math to calculate the direction in which the character should go(next path point). In here you can use lerp, sine and delta seconds to make smooth character rotation, while following the general path. I would create and test each part seperately(find path, movement input, zig zagging), so it is easier for you to debug it.

1 Like

The solution that I have found is to use a SplineComponent and make the points on a sinus shape.

Then move whatever I want along the spline. A good and straightforward tutorial => https://www.youtube.com/watch?v=qKIJMQJsx2Y

1 Like

Hi @NonoDey
Welcome to our community!

Thank for you idea… i solved it using environment query system two years ago!!
But, your idea could be useful too.

Very appreciated!!

1 Like