Enemy Spaceship Curve Movement

I want to know how i can go and make a curve movment like the image i have blow,
by using sin and cos in not getting to understand it at all. the type of game a i am making require it so i can the enemy to come in with defferent curve which i can use random.range but i don’t know how to approach making of the script.

i am trying my best and still ain’t making any progress.

You probably don’t want to use sin and cos for a “curvy path” movement. Sin and cos would be useful if you want a repeating wave pattern, but if I’m understanding the kind of movement you want, you want to be able to specify a softly curving path for your enemies to move along. Correct?

In come Bezier curves to the rescue! A Bezier curve allows you to specify a beginning and ending point, as well as a third point in the middle that influences the curvature of the line. I’ll link you to the Wikipedia article, which makes Bezier curves look a lot more complicated than they are. Ignore all the math formulas here, and just check out the pretty animated gifs, because Bezier curves are like the easiest thing ever to implement.

So you’re familiar with Lerp, right? You feed Vector3.Lerp 2 points and a number from 0 to 1; it returns a Vector3 between the two, based on the number you feed it. On the Wikipedia page, this is shown under the “Linear curve” section.

Alright, so now let’s add a third point in the middle, so now we have A, B, and C. How do we implement this? It’s really, really easy:

Vector3 AtoB = Vector3.Lerp(A, B, t);
Vector3 BtoC = Vector3.Lerp(B, C, t);
Vector3 final = Vector3.Lerp(AtoB, BtoC, t);

And that’s it. Wrap that code up in a function that takes A, B, C, and t; voila, you’ve made a Bezier curve. You can add another level on top of that to take four points (most of the Bezier curve editors you use, e.g. the pen tool in Photoshop or Illustrator, use four points); you collapse the four points down to three in the exact same way that the first two lines there collapse the three down to two. You can also make it take an arbitrary number of input points and recursively work it down step by step to a single final point, but that’s not necessary right now.

So now, you take this Bezier function. You can now implement movement using this function in place of Lerp, and use this to make the movement smooth and curvy. Each enemy has a list of points, which get fed in two at a time (they become B and C, the current position becomes A).

4 Likes

so sometime if i create this can the enemy not going to rite rotation has the beerier curve now
how can i create that.
and also i can the enemy to random change movement every time a enemy is spawn.
remember the snake movement image with the plane that i have above, how can i create make between enemy when spawn cause i find my enemy spawn close to each

To get the enemy to the correct rotation, you just need to use Quaternion.LookRotation. It can be as simple as:

transform.rotation = Quaternion.LookRotation( Bezier(A, B, C, t+0.01f) - Bezier(A, B, C, t) );

Alternately, you could simply store your previous position before you change it, and then use Quaternion.LookRotation(newPosition - oldPosition)

Simply randomize the points in the array.

For snake-like following, you just need to have all the enemies in the snake have identical points, and starting at a slightly different time.

you know i don’t understand how to create the script i trying me best but i can think a way to put the little information you give me a create the script rite now i still looking for more script cause i don’t want my space shooter game look lame on the play store, only the enemy movement kill me out

Hell ya, best Bezier curve tutorial on the internet.

A more retro way that avoids Bezier curves is to base the movement on circular arcs and straight lines (for each circular arc, the radius defines how ‘tight’ or wide is the turn).
You can calculate the position based on the equation of a circle.
But ideally it involves a simple editor so you can visualise your curves (made of straight line and arc sections), or at the very least a way to enter the data that makes sense to you and allows you to create the curves that you want.