CannonController.cs 1.26 KB
Newer Older
Markus Schlösser's avatar
Markus Schlösser committed
1
using UnityEngine;
2

3
public class CannonController : MonoBehaviour, IResetable {
Markus Schlösser's avatar
Markus Schlösser committed
4
5
	[SerializeField] private GameObject cannonBarrel;
	[SerializeField] private GameObject fire;
6
	[SerializeField] private float fireDelay;
7
8
    [SerializeField] private float phase;
    [SerializeField] private float speed = 10f;
9
	[SerializeField] private Animator anim;
10
11
	[SerializeField] private Animator animRadVorne;
	[SerializeField] private Animator animRadHinten;
12
	[SerializeField] private bool ignoreEnviroment;
Markus Schlösser's avatar
Markus Schlösser committed
13

14
15
16
17
18
19
	private Coroutine co;

	private float time;

	private void Start() {
		ResetToLevelBegin();
Jan Meissner's avatar
Jan Meissner committed
20
	}
21
22
23
24
25
26
27
28
29
30
31

	private void FixedUpdate() {
		if (time >= fireDelay) {
			time -= fireDelay;
			Fire();
		}

		time += Time.deltaTime;
	}

	private void Fire() {
32
33
		anim.SetTrigger("Fire");
		
Markus Schlösser's avatar
Markus Schlösser committed
34
		GameObject obj = Instantiate(fire, cannonBarrel.transform.Find("FirePoint").position, cannonBarrel.transform.rotation);
35
		// Drehung
Markus Schlösser's avatar
Markus Schlösser committed
36
37
		obj.transform.rotation = Quaternion.Euler(0, 0, 90) * cannonBarrel.transform.rotation;
		obj.transform.localScale = transform.lossyScale;
38
		// Flugrichtung
Markus Schlösser's avatar
Markus Schlösser committed
39
		obj.GetComponent<Rigidbody2D>().velocity = cannonBarrel.transform.right * speed;
40
		obj.GetComponent<ProjektilController>().ignoreEnviroment = ignoreEnviroment;
41
	}
42
43

	public void ResetToLevelBegin() {
44
		time = phase;
45
	}
Jan Meissner's avatar
Jan Meissner committed
46
}