/
RayShooter.cs
99 lines (88 loc) · 3.09 KB
/
RayShooter.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
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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class RayShooter : MonoBehaviour
{
private Camera _camera;
private Texture2D crossHairImage;
bool showingCursor;
private GunSwap gs;
AudioClip gunAudio;
AudioSource audioSource;
Animation _animation;
void Start()
{
_camera = GetComponent<Camera>();
}
void OnGUI()
{
int size = 12;
float posX = _camera.pixelWidth / 2 - size / 4;
float posY = _camera.pixelHeight / 2 - size / 2;
GUI.Label(new Rect(posX, posY, size, size), crossHairImage);
gs = GetComponent<GunSwap>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
GunSwap gs = player.GetComponentInChildren<GunSwap>();
if (gs.getCurrentWeaponIndex() == 0)
{
Managers.Audio.PlaySound(Resources.Load("Music/pistol_shot") as AudioClip);
}
if(gs.getCurrentWeaponIndex() == 1)
{
Managers.Audio.PlaySound(Resources.Load("Music/pistol2") as AudioClip);
}
if (!EventSystem.current.IsPointerOverGameObject())
{
Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject hitObject = hit.transform.gameObject;
ReactiveTarget target = hitObject.GetComponent<ReactiveTarget>();
if (target != null)
{
Debug.Log(target.tag);
if (target.tag == "Enemy")
{
target.ReactToHit();
}
if (target.tag == "Boss")
{
target.BossReactToHit();
}
}
else
{
StartCoroutine(SphereIndicator(hit.point));
}
}
}
}
}
private IEnumerator SphereIndicator(Vector3 pos)
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
GunSwap gs = player.GetComponentInChildren<GunSwap>();
if(gs.getCurrentWeaponIndex()==0)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(sphere);
}
else if(gs.getCurrentWeaponIndex()==1)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(cube);
}
}
}