summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs
diff options
context:
space:
mode:
Diffstat (limited to 'SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs')
-rw-r--r--SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs b/SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs
new file mode 100644
index 0000000..5d2a5bb
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs
@@ -0,0 +1,73 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+/// <summary>
+/// 回旋镖
+/// </summary>
+public class Boomerang : MonoBehaviour
+{
+ public BoomerangAfterImage m_AfterImage;
+
+ private Vector2 m_Direction;
+
+ // 当前速度
+ private float m_Speed;
+
+ // 最大飞行距离
+ private float m_MaxDist;
+
+ private float m_RotateSpeed = 500f;
+
+ Coroutine m_CoAfterImage;
+
+ public void Set(Vector2 dir, float speed, float maxDist)
+ {
+ m_Direction = dir;
+ m_Speed = speed;
+ m_MaxDist = maxDist;
+
+ m_CoAfterImage = StartCoroutine(CoAfterImage(0.05f));
+ }
+
+ private void Update()
+ {
+ Vector2 move = m_Direction * m_Speed * Time.deltaTime;
+
+ m_MaxDist -= move.magnitude;
+ if(m_MaxDist <= 0)
+ {
+ Destroy(this.gameObject);
+
+ if (m_CoAfterImage != null)
+ {
+ StopCoroutine(m_CoAfterImage);
+ }
+
+ return;
+ }
+
+ Vector3 pos = transform.position;
+ pos += new Vector3(move.x, move.y, 0);
+
+ transform.position = pos;
+
+ transform.rotation *= Quaternion.Euler(0,0,m_RotateSpeed * Time.deltaTime);
+ }
+
+ IEnumerator CoAfterImage(float interval)
+ {
+ while (true)
+ {
+ BoomerangAfterImage afterImage = BoomerangAfterImage.Instantiate(m_AfterImage);
+ afterImage.Set(this);
+ afterImage.transform.position = this.transform.position;
+ afterImage.transform.localScale= new Vector3(0.5f, 0.5f, 1f);
+ afterImage.transform.rotation = this.transform.rotation;
+ afterImage.gameObject.SetActive(true);
+
+ yield return new WaitForSeconds(interval);
+ }
+ }
+
+} \ No newline at end of file