Player_FireAttack.cs 1.24 KB
Newer Older
mthiele2's avatar
mthiele2 committed
1 2 3 4
using UnityEngine;
using System.Collections.Generic;
using System.Collections;

mthiele2's avatar
mthiele2 committed
5
public class Player_FireAttack : MonoBehaviour
mthiele2's avatar
mthiele2 committed
6
{
mthiele2's avatar
mthiele2 committed
7
	private float nextFire;
8
	public float cooldown;
mthiele2's avatar
mthiele2 committed
9
	public int volleySize;
mthiele2's avatar
mthiele2 committed
10
	public float launchPower;
mthiele2's avatar
mthiele2 committed
11 12
	public float attackSpeed;

mthiele2's avatar
mthiele2 committed
13 14
	public Transform shotSpawn;
	public GameObject fireBall;
mthiele2's avatar
mthiele2 committed
15 16

	private Rigidbody2D body;
17 18
	private bool _ready;
	private bool _activated;
mthiele2's avatar
mthiele2 committed
19

mthiele2's avatar
mthiele2 committed
20
	private void Start()
mthiele2's avatar
mthiele2 committed
21
	{
mthiele2's avatar
mthiele2 committed
22
		nextFire = 0;
23 24
		_ready = true;
		_activated = false;
mthiele2's avatar
mthiele2 committed
25 26
	}

mthiele2's avatar
mthiele2 committed
27
	private void Update()
mthiele2's avatar
mthiele2 committed
28
	{
29
		_ready = Time.time > nextFire;
mthiele2's avatar
mthiele2 committed
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44
		if (!_activated) return;

		print("Fire_Attack");
		nextFire = Time.time + cooldown;

		StartCoroutine(fire());

		_activated = false;
		//if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
		//{
		//	nextFire = Time.time + Cooldown;

		//	StartCoroutine(fire());
		//}
mthiele2's avatar
mthiele2 committed
45 46
	}

47 48 49 50 51 52
	public bool Attack()
	{
		if (_ready)
		{
			_activated = true;
		}
mthiele2's avatar
mthiele2 committed
53

54 55
		return _ready;
	}
mthiele2's avatar
mthiele2 committed
56

mthiele2's avatar
mthiele2 committed
57
	IEnumerator fire()
mthiele2's avatar
mthiele2 committed
58 59 60
	{
		for (int i = 0; i < volleySize; i++)
		{
mthiele2's avatar
mthiele2 committed
61 62 63
			GameObject projectile = Instantiate(fireBall, shotSpawn.position, shotSpawn.rotation);
			Vector3 direction = shotSpawn.position - transform.position;
			projectile.GetComponent<Rigidbody2D>().velocity = direction * launchPower;
mthiele2's avatar
mthiele2 committed
64 65 66 67
			yield return new WaitForSeconds(attackSpeed);
		}
	}
}