miepzerino
2025-03-29 ad79d9ca49274cc660fc2030a071b24314f0f210
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Assets.Scripts.Enums;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Pickup : MonoBehaviour
{
    public Item item;
 
    public Vector3 spinRotatationSpeed = new Vector3(0, 180, 0);
 
    public SoundName soundName;
 
 
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Collider2D collider = collision.collider;
        // get <Inventory> object if collision object has it
        Inventory inventory = collider.GetComponent<Inventory>();
        Debug.Log("Pickup collected by: " + collision.gameObject);
        Debug.Log("Inventory: " + inventory);
        // OnTrigger with <Inventory>
        if (inventory)
        {
            // Pickup health if not at max HP
            if (inventory.AddItem(item))
            {
                SoundManager.instance.PlaySoundAtPoint(gameObject, soundName);
                Destroy(gameObject);
            }
        }
    }
 
    //private void OnTriggerStay2D(Collider2D collision)
    //{
    //    // pickup while inside collision
    //    OnCollisionEnter2D(collision);
    //}
 
    private void Update()
    {
        // rotate pickup object
        transform.eulerAngles += spinRotatationSpeed * Time.deltaTime;
    }
}