|

Projekt Unity: 2D Shopper

Link do 2D Shopper Assets

using UnityEngine;

public class Cart : MonoBehaviour
{
    Rigidbody2D _rb;
    readonly float _speed = 11f;
    float _dirx;

    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        _dirx = Input.GetAxisRaw("Horizontal");
    }

    void FixedUpdate()
    {
        Move();
    }

    void Move()
    {
        _rb.linearVelocity = new(_dirx * _speed, _rb.linearVelocity.y);
    }
}
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI refPointsTxt;
    int _points = 0;

    public void GameOver()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void AddPoint()
    {
        _points++;
        refPointsTxt.text = _points.ToString();
    }
}
using UnityEngine;

public class Item : MonoBehaviour
{
    [SerializeField] Sprite[] refSprites;
    GameManager _refGameManager;

    void Start()
    {
        GetComponent<SpriteRenderer>().sprite = GetRandomSprite();
    }

    public void IntializeItem(GameManager refGM)
    {
        _refGameManager = refGM;
    }
    Sprite GetRandomSprite()
    {
        int rand = Random.Range(0, refSprites.Length);
        return refSprites[rand];
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.GetComponent<Cart>() != null)
        {
            _refGameManager.AddPoint();
            Destroy(gameObject);
        }
        else
        {
            _refGameManager.GameOver();
        }
    }
}
using UnityEngine;

public class ItemsSpawner : MonoBehaviour
{
    [SerializeField] GameManager refGameManager;
    [SerializeField] GameObject refItemPrefab;

    float _spawnDelay = 1;
    float _counter = 0;


    void CreateItem()
    {
        Vector3 pos = new(Random.Range(-7f, 7f), 5f, 0f);
        GameObject item = Instantiate(refItemPrefab, pos, Quaternion.identity);
        item.GetComponent<Item>().IntializeItem(refGameManager);
        _spawnDelay = 1 - (_counter*0.005f);
    }

    void Update()
    {
        _counter += Time.deltaTime;
        if (_counter > _spawnDelay)
        {
            CreateItem();
            _counter = 0;
        }
    }
}

Podobne wpisy

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *