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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(5) では Tower.cs の継承元となっている DamageableBehaviour.cs スクリプトの解説を行った。今回は更に Tower.cs の継承元となっている Targetable.cs の解説を行っていく。
「Tower.cs」 は 「Targetable.cs」 を継承し、「Targetable.cs」 は 「DamageableBehaviour.cs」 を継承している。
前回は「DamageableBehaviour.cs」の行った。続いて「Targetable.cs」 を行っていく。DamageableBehaviour.cs の解説についてはこちらを参照してほしい。
1.タワー編 – Targetable.cs
Targetable.cs について稚拙ながら解説
「Targetable.cs」は「Asset/Scripts/ActionGameFramework/Health/Targetable.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 using Core.Health;
using UnityEngine;
namespace ActionGameFramework.Health
{
/// <summary>
/// A simple class for identifying enemies
/// </summary>
public class Targetable : DamageableBehaviour
{
/// <summary>
/// The transform that will be targeted
/// </summary>
public Transform targetTransform;
/// <summary>
/// The position of the object
/// </summary>
protected Vector3 m_CurrentPosition, m_PreviousPosition;
/// <summary>
/// The velocity of the rigidbody
/// </summary>
public virtual Vector3 velocity { get; protected set; }
/// <summary>
/// The transform that objects target, which falls back to this object's transform if not set
/// </summary>
public Transform targetableTransform
{
get
{
return targetTransform == null ? transform : targetTransform;
}
}
/// <summary>
/// Returns our targetable's transform position
/// </summary>
public override Vector3 position
{
get { return targetableTransform.position; }
}
/// <summary>
/// Initialises any DamageableBehaviour logic
/// </summary>
protected override void Awake()
{
base.Awake();
ResetPositionData();
}
/// <summary>
/// Sets up the position data so velocity can be calculated
/// </summary>
protected void ResetPositionData()
{
m_CurrentPosition = position;
m_PreviousPosition = position;
}
/// <summary>
/// Calculates the velocity and updates the position
/// </summary>
void FixedUpdate()
{
m_CurrentPosition = position;
velocity = (m_CurrentPosition - m_PreviousPosition) / Time.fixedDeltaTime;
m_PreviousPosition = m_CurrentPosition;
}
}
}
9行目 : 「public class Targetable : DamageableBehaviour」これは前回解説した「DamageableBehaviour」を継承していることを指している。「DamageableBehaviour」についてはこちらを参照してほしい。
48行目 : 「protected override void Awake()」では前回の Awake と同様に起動時の初期化処理を行っている。初期化の内容としては「DamageableBehaviour」の Awake 処理と位置データの初期化である。
また、「DamageableBehaviour」の Awake では virtual が付いており、前回記載した通り関数の書き換えを行うことができる。今回は override と付いているがこれは virtual の関数を書き換えていることを意味している。
さらに、 処理の内部で base.Awake(); という処理があるがこれは継承元、つまり「DamageableBehaviour」の Awake を実行するという意味となっている。
66行目 : 「void FixedUpdate()」 は Unity で定義されている関数の一つであり、一秒間に一定回数の更新がかかる Update 処理である。 Update と FixedUpdate の違いは不規則な更新とフレーム管理されている更新という違いがある。この内容についてはこちらのサイト様が参考になるので合わせて参照してほしい。
この「Targetable.cs」は「DamageableBehaviour.cs」を継承していることから、表示されている関数以外にも 「DamageableBehaviour.cs」で定義した関数が使用できるようになっている。