From 09c0b2eed74a77010ba686061b147c9cace88ed6 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sat, 7 Oct 2017 22:57:48 +0200 Subject: Trur æ la te VCS, ikke meninga, men æ trur itj de e nå problem egt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- td/Assets/Scripts/Projectile.cs | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 td/Assets/Scripts/Projectile.cs (limited to 'td/Assets/Scripts/Projectile.cs') 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); + } + +} -- cgit v1.2.3