Files
Crash-Course/Assets/Scripts/UseItem.cs

64 lines
1.5 KiB
C#
Raw Normal View History

2025-02-17 19:02:14 -05:00
using UnityEngine;
public class UseItem : MonoBehaviour
{
2025-03-17 18:20:03 -04:00
[SerializeField] private string itemTag;
private GameObject heldItem;
private bool isHoldingItem = false;
void Update()
2025-02-17 19:02:14 -05:00
{
if (isHoldingItem)
{
2025-03-17 18:20:03 -04:00
heldItem.transform.position = transform.position + Vector3.up;
}
2025-02-17 19:02:14 -05:00
}
2025-03-17 18:20:03 -04:00
private void OnCollisionEnter2D(Collision2D collision)
{
2025-03-17 18:20:03 -04:00
if (collision.gameObject.CompareTag("Hat") && !isHoldingItem)
{
PickUpItem(collision.gameObject);
}
}
private void PickUpItem(GameObject item)
{
heldItem = item;
isHoldingItem = true;
item.GetComponent<Collider2D>().enabled = false;
2025-03-17 18:20:03 -04:00
item.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
item.transform.rotation = Quaternion.identity;
}
public void DropItem()
{
if (isHoldingItem)
{
heldItem.GetComponent<Collider2D>().enabled = true;
2025-03-17 18:20:03 -04:00
heldItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
2025-03-17 19:59:41 -04:00
heldItem.transform.position += Vector3.up * 3f;
heldItem = null;
isHoldingItem = false;
}
}
private void OnEnable()
{
2025-03-17 18:20:03 -04:00
//Punch.OnPlayerPunched += HandlePlayerPunched;
}
private void OnDisable()
{
2025-03-17 18:20:03 -04:00
//Punch.OnPlayerPunched -= HandlePlayerPunched;
}
2025-03-17 18:20:03 -04:00
/*
private void HandlePlayerPunched(GameObject punchedPlayer)
2025-02-17 19:02:14 -05:00
{
if (punchedPlayer == gameObject)
{
DropItem();
}
2025-03-17 18:20:03 -04:00
}*/
2025-02-17 19:02:14 -05:00
}