-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniDrone.cs
64 lines (53 loc) · 1.76 KB
/
MiniDrone.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
/*
Code for a physics-based flight sim shooter by Ian Snyder
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CodeStage.AntiCheat.ObscuredTypes;
public class MiniDrone : TargetableEntity {
public float followSpeed = 100f;
public ObscuredInt numCoins = 1;
void OnEnable(){
// Stuff to reset when using the pool manager
gameObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
alive = true;
}
protected override void StartCustom () {
alive = true;
}
protected override void DieCustom(string dieFromWhat = "a bullet", EnemyType.Type fromType = EnemyType.Type.Turret)
{
alive = false;
SFGameManager.instance.vfxShipExplosionPool.TryGetNextObject (transform.position, Quaternion.identity);
if (faction == Faction.Empire)
{
SFGameManager.instance.RemoveEnemy(this);
}
// COINS
if (fromType != EnemyType.Type.Terrain) SFGameManager.instance.SpawnCoins(numCoins, rb.position);
gameObject.SetActive(false);
}
void FixedUpdate(){
transform.LookAt (CalculateLead());
rb.AddForce (transform.forward * followSpeed);
}
Vector3 CalculateLead () {
Vector3 targetPos = SFGameManager.instance.players[0].transform.position;
Vector3 V = SFGameManager.instance.players[0].GetComponent<Rigidbody>().velocity;
Vector3 D = targetPos - transform.position;
float A = V.sqrMagnitude - followSpeed * followSpeed;
float B = 2 * Vector3.Dot (D, V);
float C = D.sqrMagnitude;
if (A >= 0) {
//Debug.LogError ("No solution exists");
return targetPos;
} else {
float rt = Mathf.Sqrt (B*B - 4*A*C);
float dt1 = (-B + rt) / (2 * A);
float dt2 = (-B - rt) / (2 * A);
float dt = (dt1 < 0 ? dt2 : dt1);
return targetPos + V * dt;
}
}
}