• We just launched and are currently in beta. Join us as we build and grow the community.

Creating Enemy and Structures

thenewbie123

Laugh Riot
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
158
Likes
31
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Operating System

Android

Creating Enemy

We will now create the object that will be target for the cannon. To do that first locates the sprite inside the Sprites directory.

  1. Drag the sprite GameObject to the Scene View.
    69_1.png

  2. Next set the component of the Enemy GameObject.
    70_1.png

    Note: To the other enemies sprites make you sure the transform components is same above the image.
  3. Next select the Enemy then add an Animator Controller in the Inspector.
  4. Go to Animations and create a new directory that correspond to your enemy sprite. In the Sprites directory they are type of enemies the blue, green and yellow. So create a directory that base on the enemy properties as Blue, Green and Yellow.
  5. Then click Create New Clip and name it as correspond to your enemy name, in my example I used name it as Blue Idle and save inside Animations corresponding directory to avoid misplaced.
  6. Then click again the Create New Clip to create another animations and name it as Blue Hurt and save it in the Animations corresponding directory. After that set each Animation Clip components shown below.

    Blue Idle

    71_1.png

    Blue Hurt

    72_1.png

  7. After that we will create the script for the enemies. Go to the Scripts folder and create a new folder inside of it called Enemy.
  8. Create a C# script called EnemyController.
  9. Write these block of code inside the EnemyController Class:
    1. public

      int

      maxHitPoints;
    2. public

      float

      damageCounter;
    3. public

      GameObject deathEffect;
    4. public

      AudioClip hurt;

    5. private

      Animator animator;
    6. private

      int

      hitPoints;
    7. private

      Vector3 bounce;
    8. private

      int

      maxScore;

    9. void

      Awake(

      )

      {
    10. animator =

      GetComponent<

      Animator>

      (

      )

      ;
    11. }

    12. // Use this for initialization
    13. void

      Start (

      )

      {
    14. InitializeVariables (

      )

      ;
    15. }

    16. // Update is called once per frame
    17. void

      Update (

      )

      {

    18. }

    19. void

      InitializeVariables(

      )

      {
    20. hitPoints =

      maxHitPoints;
    21. maxScore =

      1000

      ;
    22. }

    23. void

      UpdateAnimationState(

      )

      {
    24. if

      (

      hitPoints <=

      5

      )

      {
    25. animator.

      SetTrigger

      (

      "isDamage"

      )

      ;
    26. }
    27. }

    28. void

      OnCollisionEnter2D(

      Collision2D collision)

      {
    29. if

      (

      collision.

      relativeVelocity

      .

      magnitude

      >

      damageCounter)

      {
    30. hitPoints -=

      Mathf.

      RoundToInt

      (

      collision.

      relativeVelocity

      .

      magnitude

      )

      ;
    31. UpdateScoreStatus (

      Mathf.

      RoundToInt

      (

      collision.

      relativeVelocity

      .

      magnitude

      )

      )

      ;
    32. if

      (

      GameController.

      instance

      !=

      null

      &&

      MusicController.

      instance

      !=

      null

      )

      {
    33. if

      (

      GameController.

      instance

      .

      isMusicOn

      )

      {
    34. if

      (

      gameObject !=

      null

      )

      {
    35. AudioSource.

      PlayClipAtPoint

      (

      hurt, transform.

      position

      )

      ;
    36. }
    37. }
    38. }
    39. }


    40. UpdateAnimationState (

      )

      ;

    41. if

      (

      hitPoints <=

      0

      )

      {
    42. Death (

      )

      ;

    43. if

      (

      collision.

      gameObject

      .

      CompareTag

      (

      "Player Bullet"

      )

      )

      {
    44. bounce =

      collision.

      transform

      .

      GetComponent

      <

      Rigidbody2D>

      (

      )

      .

      velocity

      ;
    45. bounce.

      y

      =

      0f;
    46. collision.

      transform

      .

      GetComponent

      <

      Rigidbody2D>

      (

      )

      .

      velocity

      =

      bounce;

    47. }
    48. }
    49. }

    50. void

      Death(

      )

      {
    51. Destroy (

      gameObject)

      ;
    52. GameObject newDeathEffect =

      Instantiate (

      deathEffect, transform.

      position

      , Quaternion.

      identity

      )

      as

      GameObject;
    53. Destroy (

      newDeathEffect, 3f)

      ;
    54. if

      (

      GameController.

      instance

      !=

      null

      )

      {
    55. GameController.

      instance

      .

      score

      +=

      maxScore;
    56. }
    57. DisplayScore (

      )

      ;

    58. }

    59. void

      UpdateScoreStatus(

      int

      hitScore)

      {
    60. if

      (

      GameController.

      instance

      !=

      null

      )

      {
    61. GameController.

      instance

      .

      score

      +=

      hitScore;
    62. }
    63. }

    64. void

      DisplayScore(

      )

      {
    65. GameObject scoreText =

      Instantiate (

      Resources.

      Load

      (

      "Score Text Canvas"

      )

      , new

      Vector3(

      transform.

      position

      .

      x

      , transform.

      position

      .

      y

      +

      1f)

      , Quaternion.

      identity

      )

      as

      GameObject;
    66. scoreText.

      transform

      .

      GetChild

      (

      0

      )

      .

      transform

      .

      GetComponent

      <

      Text>

      (

      )

      .

      text

      =

      maxScore.

      ToString

      (

      )

      ;
    67. Destroy (

      scoreText, 2f)

      ;
    68. }
  10. After creating the script, drag the script to the Blue inspector as a component.
  11. We will create now the death effect for the enemy go to GameObject and Select Particle System.
  12. Rename the Particle System as EnemyDeathEffect and set the components of the Particle System as shown below.
    73_1.png

  13. After creating the effect, drag the effects inside the FX directory to create a prefab and delete it because we will just spawn the gameobject.
  14. Lastly set the EnemyController script in the inspector as shown below.
    74_1.png


Creating Structure

We will now create the object that will be deflecting any cannon bullets that try to destroy enemy. To do that first locates the sprite inside the Sprites directory.

  1. Drag the sprite GameObject to the Scene View
    75_1.png


    Then rename the GameObject as Wood.
  2. After that we will create the script for the structure. Go to the Scripts folder and create a new folder inside of it called Object.
  3. Create a C# script called Structure.
  4. Write these block of code inside the Structure Class:
    1. public

      int

      maxHitPoints;
    2. public

      Sprite[

      ]

      sprite;
    3. public

      float

      damageCounter;
    4. public

      GameObject disassemble;
    5. public

      AudioClip stoneSound, woodSound, glassSound;
    6. public

      enum

      StructureType{
    7. Wood,
    8. Stone,
    9. Glass
    10. }

    11. public

      StructureType structure;

    12. public

      int

      hitpoints;
    13. private

      SpriteRenderer spriteRenderer;
    14. private

      Vector3 bounce;
    15. private

      int

      maxScore;
    16. private

      int

      counter;

    17. void

      Awake(

      )

      {
    18. spriteRenderer =

      GetComponent<

      SpriteRenderer>

      (

      )

      ;
    19. }

    20. // Use this for initialization
    21. void

      Start (

      )

      {
    22. InitializeVariables (

      )

      ;
    23. }

    24. // Update is called once per frame
    25. void

      Update (

      )

      {
    26. }

    27. void

      InitializeVariables(

      )

      {
    28. hitpoints =

      maxHitPoints;
    29. maxScore =

      500

      ;
    30. counter =

      2

      ;
    31. }

    32. void

      OnCollisionEnter2D(

      Collision2D collision)

      {
    33. if

      (

      collision.

      relativeVelocity

      .

      magnitude

      >

      damageCounter)

      {
    34. hitpoints -=

      Mathf.

      RoundToInt

      (

      collision.

      relativeVelocity

      .

      magnitude

      )

      ;
    35. UpdateScoreStatus (

      Mathf.

      RoundToInt

      (

      collision.

      relativeVelocity

      .

      magnitude

      )

      )

      ;
    36. }


    37. if

      (

      hitpoints <=

      50

      )

      {
    38. spriteRenderer.

      sprite

      =

      sprite [

      0

      ]

      ;
    39. if

      (

      counter ==

      2

      )

      {
    40. AudioManager (

      )

      ;
    41. counter--;
    42. }

    43. }

    44. if

      (

      hitpoints <=

      30

      )

      {
    45. spriteRenderer.

      sprite

      =

      sprite [

      1

      ]

      ;
    46. if

      (

      counter ==

      1

      )

      {
    47. AudioManager (

      )

      ;
    48. counter--;
    49. }
    50. }


    51. if

      (

      hitpoints <=

      0

      )

      {
    52. Destroyed (

      )

      ;

    53. if

      (

      collision.

      gameObject

      .

      CompareTag

      (

      "Player Bullet"

      )

      )

      {
    54. bounce =

      collision.

      transform

      .

      GetComponent

      <

      Rigidbody2D>

      (

      )

      .

      velocity

      ;
    55. bounce.

      y

      =

      0f;
    56. collision.

      transform

      .

      GetComponent

      <

      Rigidbody2D>

      (

      )

      .

      velocity

      =

      bounce;
    57. }
    58. }
    59. }

    60. void

      Destroyed(

      )

      {
    61. Destroy (

      gameObject)

      ;
    62. GameObject newDisassemble =

      Instantiate (

      disassemble, transform.

      position

      , Quaternion.

      identity

      )

      as

      GameObject;
    63. Destroy (

      newDisassemble, 3f)

      ;
    64. if

      (

      GameController.

      instance

      !=

      null

      )

      {
    65. GameController.

      instance

      .

      score

      +=

      maxScore;
    66. }

    67. GameObject scoreText =

      Instantiate (

      Resources.

      Load

      (

      "Score Text Canvas"

      )

      , new

      Vector3 (

      transform.

      position

      .

      x

      , transform.

      position

      .

      y

      +

      1f, transform.

      position

      .

      z

      )

      , Quaternion.

      identity

      )

      as

      GameObject;
    68. scoreText.

      transform

      .

      GetChild

      (

      0

      )

      .

      transform

      .

      GetComponent

      <

      Text>

      (

      )

      .

      text

      =

      maxScore.

      ToString

      (

      )

      ;
    69. Destroy (

      scoreText, 2f)

      ;
    70. }

    71. void

      UpdateScoreStatus(

      int

      hitScore)

      {
    72. if

      (

      GameController.

      instance

      !=

      null

      )

      {
    73. GameController.

      instance

      .

      score

      +=

      hitScore;
    74. }
    75. }

    76. void

      AudioManager(

      )

      {
    77. switch

      (

      structure)

      {
    78. case

      StructureType.

      Wood

      :
    79. if

      (

      GameController.

      instance

      !=

      null

      &&

      MusicController.

      instance

      !=

      null

      )

      {
    80. if

      (

      GameController.

      instance

      .

      isMusicOn

      )

      {
    81. if

      (

      gameObject !=

      null

      )

      {
    82. AudioSource.

      PlayClipAtPoint

      (

      woodSound, transform.

      position

      )

      ;
    83. }
    84. }
    85. }
    86. break

      ;

    87. case

      StructureType.

      Stone

      :
    88. if

      (

      GameController.

      instance

      !=

      null

      &&

      MusicController.

      instance

      !=

      null

      )

      {
    89. if

      (

      GameController.

      instance

      .

      isMusicOn

      )

      {
    90. if

      (

      gameObject !=

      null

      )

      {
    91. AudioSource.

      PlayClipAtPoint

      (

      stoneSound, transform.

      position

      )

      ;
    92. }
    93. }
    94. }
    95. break

      ;

    96. case

      StructureType.

      Glass

      :
    97. if

      (

      GameController.

      instance

      !=

      null

      &&

      MusicController.

      instance

      !=

      null

      )

      {
    98. if

      (

      GameController.

      instance

      .

      isMusicOn

      )

      {
    99. if

      (

      gameObject !=

      null

      )

      {
    100. AudioSource.

      PlayClipAtPoint

      (

      glassSound, transform.

      position

      )

      ;
    101. }
    102. }
    103. }
    104. break

      ;
    105. }
    106. }
  5. After creating the script, drag the script to the Wood inspector as a component.
  6. We will create now the effect for the wood, first go to GameObject and Select Particle System.
  7. Rename the Particle System as Wood Disassemble Effect and set the components of the Particle System as shown below.
    77_1.png

  8. After creating the effect, drag the effects inside the FX directory to create a prefab and delete it.
  9. Then set the Structure script in the inspector as shown below.
    78_1.png

  10. Lastly duplicate the Wood GameObject into two then set each components as show below.

    Wood Wall Medium

    79_1.png

    Wood Wall Long

    80_1.png


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,891

323,899

Top