summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs
blob: 69225b62bfa97fba76808f6934e64130f375c8dc (plain)
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestBeamGun : MonoBehaviour
{
	public TestBeamBullet bullet;

	public Transform start;

	public float speed = 1;

	SpriteRenderer sprite;

	// Start is called before the first frame update
	void Start()
    {
		sprite = GetComponent<SpriteRenderer>();

	//	StartCoroutine(coFire());
	}

	// Update is called once per frame
	void Update()
    {

		Vector3 screenPos = Input.mousePosition;

		Vector2 target = Camera.main.ScreenToWorldPoint(screenPos);

		Vector2 dir = Vector2.ClampMagnitude(target - (Vector2)transform.position, 1);
		Vector3 move = dir * speed * Time.deltaTime;

		float x = move.x;
		if (x > 0)
		{
			sprite.flipY = false;
		}
		else if (x < 0)
		{
			sprite.flipY = true;
		}

		transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * Mathf.Atan2(dir.y, dir.x));

		Shot();
	}

	void Shot()
	{
		if (Input.GetButtonDown("Fire1"))
		{
			//Debug.Log("Shoot");

			TestBeamBullet b = TestBeamBullet.Instantiate(bullet);
			b.gameObject.SetActive(true);

			b.transform.position = start.position;

			b.direction = transform.right; 
		}
	}

	IEnumerator coFire()
	{
		while (true)
		{
			TestBeamBullet b = TestBeamBullet.Instantiate(bullet);
			b.gameObject.SetActive(true);

			b.transform.position = start.position;

			b.direction = transform.right;

			yield return new WaitForSeconds(0.5f);
		}
	}

}