-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteorMove.cs
46 lines (39 loc) · 1.34 KB
/
meteorMove.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class meteorMove : MonoBehaviour {
public float speedToPlayer;
public int currentHealth = 1;
public GameObject explosion;
private Transform Player;
private Rigidbody rb;
AudioSource explodeMusic;
AudioClip explodeClip;
void Start ()
{
explodeMusic = GetComponent<AudioSource>();
explodeClip = explodeMusic.clip;
Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, speedToPlayer * Time.deltaTime);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Bullet")
{
currentHealth -= 1;
}
if (currentHealth <= 0)
{
explodeMusic.PlayOneShot(explodeClip);
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
gameObject.GetComponent<MeshRenderer>().enabled = false;
gameObject.GetComponent<MeshCollider>().enabled = false;
Destroy(gameObject, explodeClip.length);
Destroy(expl, 1); // delete the explosion after 3 seconds
}
}
}