miepzerino
2023-12-23 eab47305629d96d19626e10b649ba4247d1f55f5
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
using Assets.Scripts.Enums;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Pickup : MonoBehaviour
{
    public int healthRestore = 20;
 
    public Vector3 spinRotatationSpeed = new Vector3(0, 180, 0);
 
    public SoundName soundName;
 
 
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // get <Damageable> object if collision object has it
        Damageable damageable = collision.GetComponent<Damageable>();
 
        // OnTrigger with <Damageable>
        if (damageable)
        {
            // Pickup health if not at max HP
            if (damageable.Heal(healthRestore))
            {
                SoundManager.instance.PlaySoundAtPoint(gameObject, soundName);
                Destroy(gameObject);
            }
        }
    }
 
    private void OnTriggerStay2D(Collider2D collision)
    {
        // pickup while inside collision
        OnTriggerEnter2D(collision);
    }
 
    private void Update()
    {
        // rotate pickup object
        transform.eulerAngles += spinRotatationSpeed * Time.deltaTime;
    }
}