-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGun.cs
148 lines (120 loc) · 3.43 KB
/
Gun.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
public GameObject impactEffect;
public float impactForce = 30f;
RaycastHit hit;
private Animator anim;
public GameObject effect;
public bool alreadyPlayed = false;
private bool nowFire = false;
[SerializeField] private AudioClip fireSound;
private AudioSource audioSource;
public AudioSource firstTake;
void Start()
{
anim = this.GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Fire1") && !alreadyPlayed && !nowFire)
{
this.GetComponent<CameraShake>().shakeDuration = 0.4f; // Camera Shake
nowFire = true;
shoot();
}
}
void LateUpdate()
{
if (nowFire)
{
anim.SetBool("idle", false);
anim.SetBool("fire", true);
}
else
{
nowFire = false;
}
}
void shoot()
{
audioSource.clip = fireSound;
audioSource.PlayOneShot(audioSource.clip);
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) //RayCast
{
// if distance long late explosion
if (hit.distance <= 8)
{ Invoke("impactgo", 0.2f); }
else if (hit.distance <= 15)
{ Invoke("impactgo", 0.4f); }
else if (hit.distance <= 25)
{ Invoke("impactgo", 0.5f); }
else if (hit.distance <= 45)
{ Invoke("impactgo", 0.7f); }
else
{ Invoke("impactgo", 0.8f); }
}
}
void impactgo() // Impact Effect and Damage
{
Debug.Log("Name :" + hit.transform.name + "Distance :" + hit.distance);
this.GetComponent<CameraShake>().shakeDuration = 0.15f;
Target target = hit.transform.GetComponent<Target>();
wallCrack wallCrack = hit.transform.GetComponent<wallCrack>();
if (wallCrack != null)
{
wallCrack.TakeDmage(damage);
}
if (target != null)
{
target.TakeDmage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
if (impactGO != null)
{
impactGO.GetComponent<AudioSource>().Play();
}
Destroy(impactGO, 2f);// Impact Effect Destroy Self
}
public void checkReload()//animation event
{
anim.SetBool("fire", false);
anim.SetBool("idle", true);
alreadyPlayed = false;
nowFire = false;
}
public void fire() //animation event
{
if (!alreadyPlayed)
{
Explode();
alreadyPlayed = true;
}
}
void Explode() // Gun Fire Particle
{
effect.GetComponent<ParticleSystem>().Play();
}
void FirstTake()
{
firstTake.Play();
StartCoroutine(firstTakeNoFire());
}
IEnumerator firstTakeNoFire()
{
firstTake.Play();
alreadyPlayed = true;
yield return new WaitForSeconds(2); //Wait one frame
alreadyPlayed = false;
}
}