-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comet.cs
42 lines (39 loc) · 1.36 KB
/
Comet.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 UnityEngine;
public class Comet : MonoBehaviour
{
float xVel;
float yVel;
Rigidbody2D rb;
float vertExtent;
float horzExtent;
int direction; // 0 = spawn at left and move right, 1 = opposite
public AudioClip sound;
AudioSource audioSource;
public void ActivateComet()
{
gameObject.SetActive(true);
vertExtent = Camera.main.orthographicSize;
horzExtent = vertExtent * Screen.width / Screen.height;
rb = transform.gameObject.GetComponent<Rigidbody2D>();
audioSource = GameObject.Find("Audio").GetComponent<AudioSource>();
gameObject.GetComponent<TrailRenderer>().Clear();
direction = Random.Range(0, 2);
yVel = Random.Range(2f, 4f);
xVel = Random.Range(8f, 10f);
if (direction == 0)
{
transform.position = new Vector3(-horzExtent + 0.06f, Camera.main.transform.position.y + vertExtent - 2f, 5);
rb.velocity = new Vector2(xVel, yVel);
}
else
{
transform.position = new Vector3(horzExtent - 0.06f, Camera.main.transform.position.y + vertExtent - 2f, 5);
rb.velocity = new Vector2(-xVel, yVel);
}
audioSource.PlayOneShot(sound);
}
private void OnBecameInvisible()
{
gameObject.SetActive(false);
}
}