aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/Projectile.cs
blob: 0cdef16fcbff25f66b03c08098223e0a4540fa2d (plain) (blame)
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
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);
	}

}