Simple Angry birds in Unity

Zobaczmy jak za pomocą bardzo prostego skryptu stworzyć funkcjonalność gry Angry Birds w Unity.

Zasada działania:
Obiekt będzie obracał się w kierunku kursora myszy.
Po wciśnięciu LMB utworzy się nowy pocisk
Im dłużej będzie wciśnięty LMB tym z większą siłą zostanie wystrzelony pocisk
Po zwolnieniiu LMB pocisk zostaje wystrzelony
Skrzynki mają nałożony tag box. Dodatkowy skrypt na podstawie sprawdza czy zderzyły się z nią elementy o tagu box. Jeśli zderzą się z ziemią wszystkie elementy o tagu box, gra się kończy sukcesem.

Shooter

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Shooter : MonoBehaviour
{
    [SerializeField] GameObject prefab;
    [SerializeField] Transform aim;
    [SerializeField] Image sliderImg;
    Camera cam;
    float maxShootForce = 30f;
    float shootForce = 0f;
    GameObject bird;
    private void Awake()
    {
        cam = Camera.main;
    }
    void Start()
    {
        sliderImg.fillAmount = shootForce;
    }

    void Update()
    {
        Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        difference.Normalize();
        float rotZ = Mathf.Atan2(difference.y, difference.x)*Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f,0f, Mathf.Clamp(rotZ, 0f,90f));


        if (Input.GetMouseButtonDown(0))
        {
            sliderImg.fillAmount = shootForce;
            bird =  Instantiate(prefab, aim.position, Quaternion.Euler(0f,0f, rotZ), aim);
            bird.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
            
        }

        if (Input.GetMouseButton(0))
        {
            if(shootForce < maxShootForce)
            {
                shootForce += Time.deltaTime*10f;
            }
            else
            {
                shootForce = 0f;
            }
            sliderImg.fillAmount = shootForce / maxShootForce;
        }

        if (Input.GetMouseButtonUp(0))
        {
            bird.transform.SetParent(null);
            bird.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
            bird.GetComponent<Rigidbody2D>().AddForce(transform.right * shootForce, ForceMode2D.Impulse);
            shootForce = 0f;
            sliderImg.fillAmount = shootForce;
        }
    }
}

Ziemia

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Ground : MonoBehaviour
{
    GameObject[] boxy;
    List<GameObject> boxyList;
    void Start()
    {
        boxy = GameObject.FindGameObjectsWithTag("box");
        boxyList = new List<GameObject>(boxy);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.transform.CompareTag("bird") || collision.transform.CompareTag("box"))
        {
            boxyList.Remove(collision.gameObject);
            Destroy(collision.gameObject);  
        }

        if(boxyList.Count == 0)
        {
            SceneManager.LoadScene(0);
        }
    }
}

Pobierz paczkę Unity z gotowym projektem

Kursy GameDev Unity

Scroll to Top