-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinScript
64 lines (50 loc) · 1.7 KB
/
CoinScript
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class coinBombScript : MonoBehaviour
{
//Variables
Rigidbody2D rb2d;
float explosionRange = 0.7f;
public LayerMask playerLayer;
public Animator animator;
bool coinExploding;
bool coinliving = true;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
}
private void Update()
{
//Det eneste, update funktionen gør i denne kode, er at sørge for at mønten forbliver i samme position mens den eksploderer.
if (coinExploding)
{
rb2d.velocity = new Vector2(0,0);
}
}
void destroyCoin()
{
if (coinliving)
{
//Objektet sættes til at blive fjernet fra programmet om 0.15 sekunder. Animationen spilles, og skade bliver uddelt til alle objekter i playerLayer
Destroy(gameObject, 0.15f);
animator.SetTrigger("explode");
Collider2D[] explosionHits = Physics2D.OverlapCircleAll(transform.position, explosionRange, playerLayer);
foreach(Collider2D player in explosionHits)
{
player.GetComponent<respawn>().hitByCoin = true;
coinExploding = true;
}
//Denne sørger for, at eksplosionen kun sker 1 gang
coinliving = false;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
//hvis mønten rammer noget der betegnes som jord, en spiller eller en ventilator
if(collision.gameObject.layer == 10 || collision.gameObject.layer == 11 || collision.gameObject.layer == 14)
{
destroyCoin();
}
}
}