From ded822e98e8eda49618d17e53407b0df1896e539 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 22 Apr 2022 19:24:15 +0800 Subject: * rename AlienSurvival project to SurvivalTest --- SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs | 79 +++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs (limited to 'SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs') diff --git a/SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs b/SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs new file mode 100644 index 0000000..69225b6 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/Test/TestBeamGun.cs @@ -0,0 +1,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(); + + // 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); + } + } + +} -- cgit v1.1-26-g67d0