-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGunShootingScript.cs
More file actions
59 lines (49 loc) · 1.28 KB
/
GunShootingScript.cs
File metadata and controls
59 lines (49 loc) · 1.28 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunShootingScript : MonoBehaviour {
public float clipSize, remainingClip;
public Transform playerPos;
public ParticleSystem shotSparks;
public GameObject hitObject;
bool waiting;
void Start() {
remainingClip = clipSize;
shotSparks.Stop();
}
void Update() {
if(transform.parent.name == "Player") {
if(Input.GetButtonDown("Fire1")) {
shootGun();
}
if(Input.GetButtonDown("Reload")) {
reloadGun();
}
}
}
public void shootGun() {
if(remainingClip > 0) {
int layerMask = 1 << 6;
RaycastHit hit;
if(Physics.Raycast(playerPos.position, playerPos.forward, out hit, Mathf.Infinity, layerMask)) {
Debug.DrawRay(playerPos.position, playerPos.forward * hit.distance, Color.green, 2);
hitObject = hit.collider.gameObject;
Debug.Log(hitObject.name);
hitObject.GetComponent<PlayerHealth>().takeDamage(30);
}
shotSparks.Play();
waiting = false;
StartCoroutine(pauseShooting());
remainingClip -= 1;
} else {
reloadGun();
}
}
void reloadGun() {
remainingClip = clipSize;
}
IEnumerator pauseShooting() {
yield return new WaitForSeconds(0.15f);
shotSparks.Stop();
}
}