-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpOrb.cs
42 lines (35 loc) · 1.05 KB
/
ExpOrb.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExpOrb : MonoBehaviour
{
public float exp = 0;
// Start is called before the first frame update
void Start()
{
}
public IEnumerator collectAnimation(GameObject player)
{
// move towards player
while (Vector3.Distance(transform.position, player.transform.position) > 0.1f)
{
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 10 * Time.deltaTime);
yield return null;
}
}
public IEnumerator Collect(GameObject player)
{
// move towards player and wait for it to finish
yield return StartCoroutine(collectAnimation(player));
player.GetComponent<Player>().CollectExp(this);
// destroy
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
StartCoroutine(Collect(other.gameObject));
}
}
}