2292人加入学习
(1人评价)
愤怒的小鸟-Unity2023-2D游戏开发小白教程

使用Unity2023.1.1 VS2022 制作完成于2023年11月20日

价格 免费

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; } }

[展开全文]