Script reorganization and UseItem created
This commit is contained in:
@@ -2,15 +2,57 @@ using UnityEngine;
|
||||
|
||||
public class UseItem : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
private GameObject heldItem;
|
||||
private bool isHoldingItem = false;
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
if (isHoldingItem)
|
||||
{
|
||||
heldItem.transform.position = transform.position;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (collision.CompareTag("Item") && !isHoldingItem)
|
||||
{
|
||||
PickUpItem(collision.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void PickUpItem(GameObject item)
|
||||
{
|
||||
heldItem = item;
|
||||
isHoldingItem = true;
|
||||
item.GetComponent<Collider2D>().enabled = false;
|
||||
}
|
||||
|
||||
public void DropItem()
|
||||
{
|
||||
if (isHoldingItem)
|
||||
{
|
||||
heldItem.GetComponent<Collider2D>().enabled = true;
|
||||
heldItem = null;
|
||||
isHoldingItem = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Punch.OnPlayerPunched += HandlePlayerPunched;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
Punch.OnPlayerPunched -= HandlePlayerPunched;
|
||||
}
|
||||
|
||||
private void HandlePlayerPunched(GameObject punchedPlayer)
|
||||
{
|
||||
if (punchedPlayer == gameObject)
|
||||
{
|
||||
DropItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user