2D Attacks

This section based off: https://www.youtube.com/watch?v=1QfxdUpVh5I

Add time-limited trigger for attacks

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4
 5 public class CravenAttackScript : MonoBehaviour
 6 {
 7     // How frequently can we attack?
 8     public float attackTimeLimit = 0.5f;
 9
10     // Countdown timer for attacks
11     private float attackCountdownTimer = 0;
12
13     void Update()
14     {
15         // See if we can attack, via timer.
16         if (attackCountdownTimer <= 0)
17         {
18             // We can attack. See if user hit space bar.
19             if (Input.GetKey(KeyCode.Space))
20             {
21                 Debug.Log("Attack");
22                 attackCountdownTimer = attackTimeLimit;
23             }
24         }
25         else
26         {
27             // Attack timer needs count-down
28             attackCountdownTimer -= Time.deltaTime;
29         }
30     }
31 }

Do damage

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4
 5 public class CravenAttackScript : MonoBehaviour
 6 {
 7     // How frequently can we attack?
 8     public float attackTimeLimit = 0.5f;
 9
10     // Countdown timer for attacks
11     private float attackCountdownTimer = 0;
12
13     // An empty parented that says where to attack
14     public Transform attackPos;
15     // Radius of attack circle
16     public float attackRange;
17     // What layer will the enemies be on?
18     public LayerMask enemyLayer;
19     // How much damage to deal
20     public int damage = 3;
21
22     void Update()
23     {
24         // See if we can attack, via timer.
25         if (attackCountdownTimer <= 0)
26         {
27             // We can attack. See if user hit space bar.
28             if (Input.GetKey(KeyCode.Space))
29             {
30                 Debug.Log("Attack");
31                 // Reset the countdown timer
32                 attackCountdownTimer = attackTimeLimit;
33                 // What enemies did we hit?
34                 Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, enemyLayer);
35                 // Loop through each enemy we hit
36                 for(int i=0; i < enemiesToDamage.Length; i++)
37                 {
38                     // Get the enemy script attached to this object
39                     CravenEnemyScript enemyScript = enemiesToDamage[i].GetComponent<CravenEnemyScript>();
40                     // If there is an enemy script
41                     if (enemyScript)
42                     {
43                         // Damage
44                         enemiesToDamage[i].GetComponent<CravenEnemyScript>().health -= damage;
45                         // Print health levels
46                         Debug.Log(enemiesToDamage[i].GetComponent<CravenEnemyScript>().health);
47
48                         // --- ToDo: destroy enemy here when health <= 0
49                     }
50                     else
51                     {
52                         // We hit an enemy, but there's no script attached to it.
53                         Debug.Log("Enemy Script not present");
54                     }
55                 }
56             }
57         }
58         else
59         {
60             // Attack timer needs count-down
61             attackCountdownTimer -= Time.deltaTime;
62         }
63     }
64     // Used to draw a circle when we are selecting the player in the scene view
65     void OnDrawGizmosSelected()
66     {
67         Gizmos.color = Color.red;
68         Gizmos.DrawWireSphere(attackPos.position, attackRange);
69     }
70 }

Note

You’ll need: * An enemy script * Turn on gizmos in the scene view * An enemy layer * Program a change to the attackPos when user changes direction.