Creating Enemy and Structures

Submitted by: 
Operating System: 
Visitors have accessed this post 252 times.

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.
    tut69
  2. Next set the component of the Enemy GameObject.
    tut70

    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
    tut71

    Blue Hurt
    tut72

  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.  
    6. private Animator animator;
    7. private int hitPoints;
    8. private Vector3 bounce;
    9. private int maxScore;
    10.  
    11. void Awake(){
    12. animator = GetComponent<Animator> ();
    13. }
    14.  
    15. // Use this for initialization
    16. void Start () {
    17. InitializeVariables ();
    18. }
    19.  
    20. // Update is called once per frame
    21. void Update () {
    22.  
    23. }
    24.  
    25. void InitializeVariables(){
    26. hitPoints = maxHitPoints;
    27. maxScore = 1000;
    28. }
    29.  
    30. void UpdateAnimationState(){
    31. if(hitPoints <= 5){
    32. animator.SetTrigger ("isDamage");
    33. }
    34. }
    35.  
    36. void OnCollisionEnter2D(Collision2D collision){
    37. if(collision.relativeVelocity.magnitude > damageCounter){
    38. hitPoints -= Mathf.RoundToInt(collision.relativeVelocity.magnitude);
    39. UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
    40. if(GameController.instance != null && MusicController.instance != null){
    41. if(GameController.instance.isMusicOn){
    42. if (gameObject != null) {
    43. AudioSource.PlayClipAtPoint (hurt, transform.position);
    44. }
    45. }
    46. }
    47. }
    48.  
    49.  
    50. UpdateAnimationState ();
    51.  
    52. if(hitPoints <= 0){
    53. Death ();
    54.  
    55. if(collision.gameObject.CompareTag("Player Bullet")){
    56. bounce = collision.transform.GetComponent<Rigidbody2D> ().velocity;
    57. bounce.y = 0f;
    58. collision.transform.GetComponent<Rigidbody2D> ().velocity = bounce;
    59.  
    60. }
    61. }
    62. }
    63.  
    64. void Death(){
    65. Destroy (gameObject);
    66. GameObject newDeathEffect = Instantiate (deathEffect, transform.position, Quaternion.identity) as GameObject;
    67. Destroy (newDeathEffect, 3f);
    68. if(GameController.instance != null){
    69. GameController.instance.score += maxScore;
    70. }
    71. DisplayScore ();
    72.  
    73. }
    74.  
    75. void UpdateScoreStatus(int hitScore){
    76. if(GameController.instance != null){
    77. GameController.instance.score += hitScore;
    78. }
    79. }
    80.  
    81. void DisplayScore(){
    82. GameObject scoreText = Instantiate (Resources.Load ("Score Text Canvas"), new Vector3(transform.position.x, transform.position.y + 1f), Quaternion.identity) as GameObject;
    83. scoreText.transform.GetChild (0).transform.GetComponent<Text> ().text = maxScore.ToString ();
    84. Destroy (scoreText, 2f);
    85. }
  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.
    tut73
  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.
    tut74

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
    tut75

    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.  
    12. public StructureType structure;
    13.  
    14. public int hitpoints;
    15. private SpriteRenderer spriteRenderer;
    16. private Vector3 bounce;
    17. private int maxScore;
    18. private int counter;
    19.  
    20. void Awake(){
    21. spriteRenderer = GetComponent<SpriteRenderer> ();
    22. }
    23.  
    24. // Use this for initialization
    25. void Start () {
    26. InitializeVariables ();
    27. }
    28.  
    29. // Update is called once per frame
    30. void Update () {
    31. }
    32.  
    33. void InitializeVariables(){
    34. hitpoints = maxHitPoints;
    35. maxScore = 500;
    36. counter = 2;
    37. }
    38.  
    39. void OnCollisionEnter2D(Collision2D collision){
    40. if(collision.relativeVelocity.magnitude > damageCounter){
    41. hitpoints -= Mathf.RoundToInt (collision.relativeVelocity.magnitude);
    42. UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
    43. }
    44.  
    45.  
    46. if (hitpoints <= 50) {
    47. spriteRenderer.sprite = sprite [0];
    48. if(counter == 2){
    49. AudioManager ();
    50. counter--;
    51. }
    52.  
    53. }
    54.  
    55. if(hitpoints <= 30){
    56. spriteRenderer.sprite = sprite [1];
    57. if(counter == 1){
    58. AudioManager ();
    59. counter--;
    60. }
    61. }
    62.  
    63.  
    64. if(hitpoints <= 0){
    65. Destroyed ();
    66.  
    67. if(collision.gameObject.CompareTag("Player Bullet")){
    68. bounce = collision.transform.GetComponent<Rigidbody2D> ().velocity;
    69. bounce.y = 0f;
    70. collision.transform.GetComponent<Rigidbody2D> ().velocity = bounce;
    71. }
    72. }
    73. }
    74.  
    75. void Destroyed(){
    76. Destroy (gameObject);
    77. GameObject newDisassemble = Instantiate (disassemble, transform.position, Quaternion.identity) as GameObject;
    78. Destroy (newDisassemble, 3f);
    79. if(GameController.instance != null){
    80. GameController.instance.score += maxScore;
    81. }
    82.  
    83. GameObject scoreText = Instantiate (Resources.Load("Score Text Canvas"), new Vector3 (transform.position.x, transform.position.y + 1f, transform.position.z), Quaternion.identity) as GameObject;
    84. scoreText.transform.GetChild (0).transform.GetComponent<Text> ().text = maxScore.ToString();
    85. Destroy (scoreText, 2f);
    86. }
    87.  
    88. void UpdateScoreStatus(int hitScore){
    89. if(GameController.instance != null){
    90. GameController.instance.score += hitScore;
    91. }
    92. }
    93.  
    94. void AudioManager(){
    95. switch (structure) {
    96. case StructureType.Wood:
    97. if(GameController.instance != null && MusicController.instance != null){
    98. if(GameController.instance.isMusicOn){
    99. if (gameObject != null) {
    100. AudioSource.PlayClipAtPoint (woodSound, transform.position);
    101. }
    102. }
    103. }
    104. break;
    105.  
    106. case StructureType.Stone:
    107. if(GameController.instance != null && MusicController.instance != null){
    108. if(GameController.instance.isMusicOn){
    109. if (gameObject != null) {
    110. AudioSource.PlayClipAtPoint (stoneSound, transform.position);
    111. }
    112. }
    113. }
    114. break;
    115.  
    116. case StructureType.Glass:
    117. if(GameController.instance != null && MusicController.instance != null){
    118. if(GameController.instance.isMusicOn){
    119. if (gameObject != null) {
    120. AudioSource.PlayClipAtPoint (glassSound, transform.position);
    121. }
    122. }
    123. }
    124. break;
    125. }
    126. }
  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.
    tut77
  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.
    tut78
  10. Lastly duplicate the Wood GameObject into two then set each components as show below.

    Wood Wall Medium
    tut79

    Wood Wall Long
    tut80


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.