/
Gun.cs
50 lines (45 loc) · 1.28 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
TimeFlow timeFlow;
[SerializeField]
Transform gunContainer = null;
[SerializeField]
Transform firePosition = null;
[SerializeField]
GameObject bulletPrefab = null;
[SerializeField]
float fireVelocity = 1f;
float lastShot = 0f;
public float shotDelay = 0.2f;
[SerializeField] string shotSound = "";
private void Awake()
{
timeFlow = GetComponent<TimeFlow>();
lastShot = Time.time;
}
public bool CanFire()
{
return Time.time - lastShot > shotDelay;
}
public void FireGun(Vector2 targetPos)
{
PointGun(targetPos);
FireBullet();
lastShot = Time.time;
if (shotSound != "") AudioManager.instance.Play(shotSound);
}
public void PointGun(Vector2 targetPos)
{
gunContainer.up = targetPos - (Vector2)transform.position;
}
void FireBullet()
{
GameObject bullet = Instantiate(bulletPrefab);
bullet.transform.position = firePosition.position;
bullet.transform.rotation = gunContainer.rotation;
bullet.GetComponent<Rigidbody2D>().velocity = timeFlow.GetVelocity(gunContainer.up * fireVelocity);
}
}