aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/Projectile.cs
diff options
context:
space:
mode:
Diffstat (limited to 'td/Assets/Scripts/Projectile.cs')
-rw-r--r--td/Assets/Scripts/Projectile.cs42
1 files changed, 42 insertions, 0 deletions
diff --git a/td/Assets/Scripts/Projectile.cs b/td/Assets/Scripts/Projectile.cs
new file mode 100644
index 0000000..0cdef16
--- /dev/null
+++ b/td/Assets/Scripts/Projectile.cs
@@ -0,0 +1,42 @@
+using UnityEngine;
+
+public class Projectile : MonoBehaviour {
+
+ public float Speed = 70f;
+ public int PointsPerHit;
+ [Header("Scripting vars")]
+ public Player Player; // Reference to the player object, should be set when instantiating
+ private Transform _target;
+
+ public void Seek(Transform target) {
+ _target = target;
+ }
+
+
+ void Update () {
+
+ if (_target == null) {
+ Destroy (gameObject);
+ return;
+ }
+
+ Vector3 direction = _target.position - transform.position;
+ float distanceThisFrame = Speed * Time.deltaTime;
+
+ if (direction.magnitude <= distanceThisFrame) {
+ HitTarget ();
+ return;
+ }
+
+ transform.Translate (direction.normalized * distanceThisFrame, Space.World);
+
+
+ }
+
+ void HitTarget() {
+ Player.ScoreAdd (PointsPerHit);
+ Destroy (_target.gameObject);
+ Destroy (gameObject);
+ }
+
+}