Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemy Spaceship Curve Movement

Discussion in 'Scripting' started by Tatsumi-Kun, Apr 28, 2016.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    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.
    vlcsnap-2016-04-27-20h23m20s078.png vlcsnap-2016-04-27-20h24m11s550.png vlcsnap-2016-04-27-20h24m21s974.png vlcsnap-2016-04-27-20h24m33s386.png
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    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:
    Code (csharp):
    1. Vector3 AtoB = Vector3.Lerp(A, B, t);
    2. Vector3 BtoC = Vector3.Lerp(B, C, t);
    3. 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).
     
  3. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    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
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    To get the enemy to the correct rotation, you just need to use Quaternion.LookRotation. It can be as simple as:
    Code (csharp):
    1. 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.
     
  5. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    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
     
  6. LouisHong

    LouisHong

    Joined:
    Nov 11, 2014
    Posts:
    69
    Hell ya, best Bezier curve tutorial on the internet.