-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dust.cs
63 lines (56 loc) · 1.9 KB
/
Dust.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dust : MonoBehaviour
{
private GameController gameController;
private Rigidbody2D myRigidbody;
public float moveSpeed;
public float secondsOfLife;
public float speedReward = 0.25f;
private Vector2 lastVelocity;
// Start is called before the first frame update
void Start()
{
gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
myRigidbody = gameObject.GetComponent<Rigidbody2D>();
myRigidbody.velocity = transform.up * moveSpeed;
SelfDesctruct(secondsOfLife);
}
// Update is called once per frame
void Update()
{
lastVelocity = myRigidbody.velocity;
}
void OnCollisionEnter2D(Collision2D collision)
{
// If we hit a player, add score, increase player speed, and destroy this item
if (collision.gameObject.tag == "Player") {
if (gameController) {
gameController.AddScore(500);
}
collision.gameObject.GetComponent<Player>().incrementSpeed(speedReward);
Destroy(gameObject);
return;
}
// Otherwise reflect off of the other object by reflecting the
// lastVelocity off of the normal vector in the collision point
float speed = lastVelocity.magnitude;
Vector2 direction = Vector2.Reflect(lastVelocity.normalized, collision.GetContact(0).normal);
myRigidbody.velocity = direction * speed;
}
void OnBecameInvisible()
{
Destroy(gameObject);
}
// Destroys this Dust after given time
public void SelfDesctruct(float time)
{
StartCoroutine(WaitToSelfDestruct(time));
}
private IEnumerator WaitToSelfDestruct(float time)
{
yield return new WaitForSeconds(time);
Destroy(gameObject);
}
}