Unity チュートリアルのタワーディフェンステンプレートを触ってみる(16)

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(15)では砲弾に対して追加した Damager コンポーネントの解説を進めた。今回は同様に砲弾に対して追加した HitscanAttack コンポーネントに付いて解説を進めていく。
1.標的設定と砲撃編 – HitscanAttack.cs
HitscanAttack.cs について稚拙ながら解説
「HitscanAttack.cs」は「Assets/Scripts/TowerDefense/Towers/Projectiles/HitscanAttack.cs」の指しておりスクリプトについては以下の通り。内容としてはダメージを与える処理を行っている。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 using ActionGameFramework.Health;
using Core.Utilities;
using UnityEngine;
namespace TowerDefense.Towers.Projectiles
{
/// <summary>
/// Implementation of hitscan projectile
/// The principle behind this weapon is that it instantly attacks enemies
/// </summary>
[RequireComponent(typeof(Damager))]
public class HitscanAttack : MonoBehaviour
{
/// <summary>
/// The amount of time to delay
/// </summary>
public float delay;
/// <summary>
/// The delay timer
/// </summary>
protected Timer m_Timer;
/// <summary>
/// The enemy this projectile will attack
/// </summary>
protected Targetable m_Enemy;
/// <summary>
/// The Damager attached to the object
/// </summary>
protected Damager m_Damager;
/// <summary>
/// The towers projectile position
/// </summary>
protected Vector3 m_Origin;
/// <summary>
/// Configuration for pausing the timer delay timer
/// without setting Time.timeScale to 0
/// </summary>
protected bool m_PauseTimer;
/// <summary>
/// The delay configuration for the attacking
/// </summary>
/// <param name="origin">
/// The point the attack will be fired from
/// </param>
/// <param name="enemy">
/// The enemy to attack
/// </param>
public void AttackEnemy(Vector3 origin, Targetable enemy)
{
m_Enemy = enemy;
m_Origin = origin;
m_Timer.Reset();
m_PauseTimer = false;
}
/// <summary>
/// The actual attack of the hitscan attack.
/// Early returns from the method if the there is no enemy to attack.
/// </summary>
protected void DealDamage()
{
Poolable.TryPool(gameObject);
if (m_Enemy == null)
{
return;
}
// effects
ParticleSystem pfxPrefab = m_Damager.collisionParticles;
var attackEffect = Poolable.TryGetPoolable<ParticleSystem>(pfxPrefab.gameObject);
attackEffect.transform.position = m_Enemy.position;
attackEffect.Play();
m_Enemy.TakeDamage(m_Damager.damage, m_Enemy.position, m_Damager.alignmentProvider);
m_PauseTimer = true;
}
/// <summary>
/// Cache the damager component attached to this object
/// </summary>
protected virtual void Awake()
{
m_Damager = GetComponent<Damager>();
m_Timer = new Timer(delay, DealDamage);
}
/// <summary>
/// Update the m_Timer if it is available
/// </summary>
protected virtual void Update()
{
if (!m_PauseTimer)
{
m_Timer.Tick(Time.deltaTime);
}
}
}
}
54行目 : 「public void AttackEnemy(Vector3 origin, Targetable enemy)」は攻撃する敵の設定を行っている。
66行目 : 「protected void DealDamage()」は攻撃する敵に対して攻撃エフェクトを出現させた後、敵のオブジェクトに対してダメージを与えている。
88行目 : 「protected virtual void Awake()」は Unity 固有の起動処理を行っている。内容としてはダメージャーコンポーネントの取得と、待機時間用のインスタンスを生成している。
97行目 : 「protected virtual void Update()」 は Unity 固有の更新処理を行っている。内容としては待機時間ごとにイベントを発生させている。