-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.cs
163 lines (125 loc) · 4.84 KB
/
Projectile.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
public float speed;
public LayerMask whatissolid;
public string TargetTag;
public float Damage;
public float Impact;
public Vector2 CurrentDirection;
public bool CanDamage = true;
public bool CanImpact = true;
//public string SoundName;
//public float ShakeAmount;
//public float SpearForce;
//public AudioMixer secondaudio;
public GameObject HitEffect;
GameObject Hit;
//public GameObject PopUpGo;
//GameObject InstGo;
//bool InstantiatedPU = false;
public bool VisuallyDestroyed = false;
//public string HitSoundName;
//AudioManager AM;
void Awake()
{
//Damage = Damage + Random.Range(-3, +4);
//AM = FindObjectOfType<AudioManager>();
Invoke("Dest", 8f);
}
private void Start()
{
CurrentDirection = transform.right;
if (!VisuallyDestroyed)
{
if (this.GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Dynamic)
{
//this.GetComponent<Rigidbody2D>().AddForce(transform.right * SpearForce, ForceMode2D.Impulse);
}
}
}
// Update is called once per frame
void Update()
{
if (!VisuallyDestroyed)
{
if (this.GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Kinematic)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
//this.GetComponent<Rigidbody2D>().velocity = CurrentDirection * speed;
}
if (this.GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Dynamic)
{
Vector2 dir = this.GetComponent<Rigidbody2D>().velocity;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, transform.forward);
}
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, -transform.right, 0.25f, whatissolid);
if (hitInfo.collider != null)
{
//AM.Play(HitSoundName);
if (hitInfo.rigidbody != null && CanImpact)
{
if (hitInfo.rigidbody.bodyType == RigidbodyType2D.Static)
{
CanImpact = false;
/*
Vector2 BounceDir = Vector2.Reflect(this.GetComponent<Rigidbody2D>().velocity, hitInfo.normal);
//CurrentDirection = -BounceDir;
float BounceAngle = -Mathf.Atan2(BounceDir.x, BounceDir.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(BounceAngle, transform.forward);
*/
}
else
{
hitInfo.rigidbody.velocity = Vector2.zero;
hitInfo.rigidbody.AddForce(hitInfo.normal * Impact, ForceMode2D.Impulse);
CanImpact = false;
}
}
if (hitInfo.collider.GetComponent<CharacterHealth>() == true)
{
if(hitInfo.collider.gameObject.CompareTag(TargetTag) || hitInfo.collider.gameObject.CompareTag("Dummy"))
{
if (CanDamage)
{
hitInfo.collider.GetComponent<CharacterHealth>().DealDam(Damage);
CanDamage = false;
}
}
}
VisDest(new Vector3(hitInfo.point.x, hitInfo.point.y, transform.position.z), Quaternion.LookRotation(-hitInfo.normal));
}
}
public void VisDest(Vector3 Pos, Quaternion Rot)
{
this.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
this.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
this.GetComponent<Rigidbody2D>().angularVelocity = 0;
transform.GetChild(0).gameObject.SetActive(false);
CanDamage = false;
CanImpact = false;
this.GetComponent<SpriteRenderer>().enabled = false;
if (!VisuallyDestroyed)
{
//FindObjectOfType<AudioManager>().Play(SoundName);
Hit = Instantiate(HitEffect, Pos, Rot);
Hit.transform.SetParent(this.transform);
}
//CameraShaker.Instance.ShakeOnce(ShakeAmount, 4f, 0.1f, 1f);
Invoke("Dest", 3f);
VisuallyDestroyed = true;
}
public void Dest()
{
// Destroy(InstGo);
Destroy(this.gameObject);
}
public void SetVolume(float volume)
{
//secondaudio.SetFloat("NEW", volume);
}
}