using System.Collections; using System.Collections.Generic; using UnityEngine; public enum BirdStart { Waiting, BeforeShoot, AfterShoot } public class Bird : MonoBehaviour { public BirdStart state = BirdStart.BeforeShoot; //等待 发射前 发射后 public bool isMouseDown = false; public float maxDistance = 2.4f; public float flySpeed = 5; private Rigidbody2D rgd; // Start is called before the first frame update void Start() { rgd = GetComponent(); rgd.bodyType = RigidbodyType2D.Static; } // Update is called once per frame void Update() { switch (state) { case BirdStart.Waiting: break; case BirdStart.BeforeShoot: MoveControl(); break; case BirdStart.AfterShoot: break; } } //OnMouseDown OnMouseUp private void OnMouseDown() { if(state == BirdStart.BeforeShoot) { isMouseDown = true; Slingshot.Instance.StartDraw(transform); } } private void OnMouseUp() { if (state == BirdStart.BeforeShoot) { isMouseDown = false; Slingshot.Instance.EndDraw(); Fly(); } } private void MoveControl() { if(isMouseDown) { transform.position = GetMousePosition(); } } private Vector3 GetMousePosition() { Vector3 centerPosition = Slingshot.Instance.getCenterPositon(); Vector3 mp = Camera.main.ScreenToWorldPoint(Input.mousePosition); mp.z = 0; Vector3 mouseDir = mp - centerPosition; float distance = mouseDir.magnitude; if (distance > maxDistance) { mp = mouseDir.normalized * maxDistance + centerPosition; } return mp; } private void Fly() { rgd.bodyType = RigidbodyType2D.Dynamic; rgd.velocity = (Slingshot.Instance.getCenterPositon() - transform.position).normalized * flySpeed; state = BirdStart.AfterShoot; } }