-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grenade.cs
179 lines (133 loc) · 4.7 KB
/
Grenade.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grenade : MonoBehaviour
{
public enum grenadeState
{
Inactive,
Active,
Exploding
}
public grenadeState currentGrenadeState;
public float explosionDelay;
private OVRGrabbable grabbbleExplosive;
private const string grabberLeftName = "AvatarGrabberLeft";
private const string grabberRightName = "AvatarGrabberRight";
public GameObject explosionObject;
public AudioClip GrenadeActivate;
public AudioClip grenadeArming;
public AudioClip grenadeExploding;
public AudioSource grenadeSource;
public Color grenadeInactiveColor;
public Color grenadeActiveColor;
public MeshRenderer grenademesh;
public GameObject bombMesh;
public Rigidbody bombRigidbody;
public float explosionColliderActivationDelay;
public GameObject explosionCollider;
// Start is called before the first frame update
void Start()
{
grabbbleExplosive = this.gameObject.GetComponent<OVRGrabbable>();
grenademesh.materials[1].color = grenadeInactiveColor;
}
// Update is called once per frame
void Update()
{
if (grabbbleExplosive != null)
{
if (grabbbleExplosive.isGrabbed)
{
grabbbleExplosive.transform.position = grabbbleExplosive.grabbedBy.gameObject.transform.position;
grabbbleExplosive.transform.rotation = grabbbleExplosive.grabbedBy.gameObject.transform.rotation;
if(currentGrenadeState == grenadeState.Inactive)
ActivateGrenade();
}
}
}
public void ActivateGrenade()
{
//*** Left Trigger Pull
//OVRInput.Get(OVRInput.RawButton.LHandTrigger)
if (grabbbleExplosive.grabbedBy.name == grabberLeftName)
{
if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) == true && currentGrenadeState == grenadeState.Inactive)
{
currentGrenadeState = grenadeState.Active;
Armgrenade();
OVRInput.SetControllerVibration(1f, .7f, OVRInput.Controller.LTouch);
Invoke("BeginExplosion", explosionDelay);
StopControllerVibration(true, .1f);
}
/*if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) == true
&& firedWeapon == true)
{
firedWeapon = false;
FireLaser();
}*/
}
//*** Right Trigger Pull
if (grabbbleExplosive.grabbedBy.name == grabberRightName)
{
if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger) == true && currentGrenadeState == grenadeState.Inactive)
{
currentGrenadeState = grenadeState.Active;
Armgrenade();
OVRInput.SetControllerVibration(1f, .7f, OVRInput.Controller.RTouch);
Invoke("BeginExplosion", explosionDelay);
StopControllerVibration(false, .1f);
}
/*if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger) == true && firedWeapon == true)
{
firedWeapon = false;
FireLaser();
}*/
}
}
public void Armgrenade()
{
grenademesh.materials[1].color = grenadeActiveColor;
grenadeSource.PlayOneShot(GrenadeActivate);
Invoke("PlayGrenadeTimerSound", .5f);
}
public void PlayGrenadeTimerSound()
{
grenadeSource.PlayOneShot(grenadeArming);
}
public void BeginExplosion()
{
Invoke("ActivateExplosionCollider", explosionColliderActivationDelay);
grenadeSource.Stop();
currentGrenadeState = grenadeState.Exploding;
explosionObject.SetActive(true);
grenadeSource.PlayOneShot(grenadeExploding);
bombMesh.SetActive(false);
bombRigidbody.isKinematic = true;
this.gameObject.transform.localEulerAngles = Vector3.zero;
Destroy(this.gameObject, 2.5f);
}
public void ActivateExplosionCollider()
{
explosionCollider.SetActive(true);
}
public void StopControllerVibration(bool pIsLeftcontroller,float ptimeDelay = 0f)
{
if (pIsLeftcontroller)
{
Invoke("StopControllerVibrationLeft", ptimeDelay);
}
else
{
Invoke("StopControllerVibrationRight", ptimeDelay);
}
}
public void StopControllerVibrationLeft()
{
OVRInput.SetControllerVibration(0f, 0f, OVRInput.Controller.LTouch);
}
public void StopControllerVibrationRight()
{
OVRInput.SetControllerVibration(0f, 0f, OVRInput.Controller.RTouch);
}
}