RaycastController.cs
3.09 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RaycastController : MonoBehaviour
{
public float maxDistanceRay= 100f;
public static RaycastController instance;
public Text birdName;
public Transform gunFlashTarget;
public float fireRate = 1.6f;
private bool nextShot = true;
private string objName = "";
AudioSource audio;
public AudioClip[] clips;
void Awake() {
if (instance == null) {
instance = this;
}
}
public void playSound(int sound) {
audio.clip = clips[sound];
audio.Play();
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(spawnNewBird());
}
// Update is called once per frame
void Update()
{
}
public void Fire() {
if(nextShot) {
StartCoroutine(takeShot());
nextShot = false;
}
}
private IEnumerator takeShot() {
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
int layer_mask = LayerMask.GetMask("birdLayer");
if(Physics.Raycast(ray, out hit, maxDistanceRay, layer_mask)) {
//debug
objName = hit.collider.gameObject.name;
birdName.text = objName;
Vector3 birdPosition = hit.collider.gameObject.transform.position;
if(objName == "Bird_Asset(Clone)") {
GameObject Boom = Instantiate(Resources.Load("boom", typeof(GameObject))) as GameObject;
Boom.transform.position = birdPosition;
Destroy(hit.collider.gameObject);
StartCoroutine(spawnNewBird());
StartCoroutine(clearBoom());
}
}
GameObject gunFlash = Instantiate(Resources.Load("gunFlashSmoke", typeof(GameObject))) as GameObject;
gunFlash.transform.position = gunFlashTarget.position;
yield return new WaitForSeconds(fireRate);
nextShot = true;
}
private IEnumerator clearBoom() {
yield return new WaitForSeconds(1.5f);
GameObject[] smokeGroup = GameObject.FindGameObjectsWithTag("Boom");
foreach (GameObject smoke in smokeGroup) {
Destroy(smoke.gameObject);
}
}
private IEnumerator spawnNewBird() {
yield return new WaitForSeconds (3f);
//Spawn new bird
GameObject newBird = Instantiate(Resources.Load("Bird_Asset", typeof(GameObject))) as GameObject;
//Make Bird Child ImageTarget
newBird.transform.parent = GameObject.Find("ImageTarget").transform;
//Scale Bird
newBird.transform.localScale = new Vector3(10f,10f,10f);
//Random Start Position
Vector3 temp;
temp.x = Random.Range(-4.8f/2, 4.8f/2);
temp.y = Random.Range(1f/2, 5f/2);
temp.z = Random.Range(-4.8f/2, 4.8f/2);
newBird.transform.position = new Vector3(temp.x,temp.y,temp.z);
}
}