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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I make enemys like in Space Invaders

Discussion in 'Scripting' started by DasJonny, Oct 19, 2018.

  1. DasJonny

    DasJonny

    Joined:
    May 15, 2017
    Posts:
    31
    Hey there!
    I am new to programming.
    I am actually trying to create a Space-Invaders-like game. So far so good.
    I have most of the work done, but my enemies are just slowly moving down the screen at the moment.

    I want them to have different movement sets, so each "wave" of enemies shall have its own path to move, until they die

    I am not quite sure how to do this right now. Is there a simple way to do this? Or do I have to create a script that is like "move to 2 x then go up to 5y" or is there a simpler way?

    I want it to be like one of those games:


    Ty for any help :)
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Everything is easy if you know how... ;)

    Have a look at google for what you are trying to accomplish. There are lots of ways.
    Here is one.
     
  3. DasJonny

    DasJonny

    Joined:
    May 15, 2017
    Posts:
    31

    First of all TY for your answer. Guess i wrote that not that clearly

    The problem is not to spawn enemies in waves, but to make them move in a special way (for example one type of enemy moves from top to bottom, one from left to right, one does loopings and one moves diagonal)
     
  4. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    One way is to use animations. You can set their transform position and rotation in an animation and simplly play the animation when you want them to fly across your scene.
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,365
    So you already know how to make the enemies move? The problem is that they are moving in the wrong direction.

    The enemies in space invaders
    1) move to the right a certain distance
    2) then they move down a little bit
    3) then they move left that same distance
    4) then they move down a little bit

    and cycle repeats.

    You can break your movement up into these four steps and so the logic looks like this

    if I'm on step 1
    { Go right a little bit.
    Did I go far enough yet? if so then now I'm on step2, otherwise I'm still on step 1
    }

    if I'm on step 2
    { Go down a little bit. Now I'm on Step 3
    }

    if I'm on step 3
    {
    Go left a little bit.
    Did I go far enough yet? if so then now I'm on step 4, other wise I'm still on step 3
    }

    if I'm on step 4
    { Go down a little bit. Now I'm on Step 1
    }
     
  6. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    You just need to have class on enemy to control moving and when you spawn him you set in this class any number that set in switch correct move function, like this:
    Code (CSharp):
    1. public class EnemyControl : MonoBehaviour {
    2.  
    3.     public int MoveSet { get; set; }
    4.  
    5.     private void Update()
    6.     {
    7.         switch (MoveSet)
    8.         {
    9.             case 1:
    10.                 //Move set 1;
    11.                 break;
    12.             case 2:
    13.                 //Move set 2;
    14.                 break;
    15.                 //...
    16.         }
    17.     }
    18. }
    You need just spawn him on correct position and code some moving function (Its just only you what moves do you expect from them.)
    Then like this set him some number when you spawning him for example:
    Code (CSharp):
    1. GameObject spawnedEnemy = Instantiate(enemy, position, rotation);
    2. spawnedEnemy.GetComponent<EnemyControl>().MoveSet = 1;
    And of course your EnemySpawn script have some timeing functions where you set when and how many of enemies you want to spawn.

    Its really easy when you know how :)