blob: 3b02763539b9a3eb317836cf60761a3a138b2b80 (
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
43
|
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() {
WaveSpawner.EnemiesAlive--;
Player.ScoreAdd (PointsPerHit);
Destroy (_target.gameObject);
Destroy (gameObject);
}
}
|