Train-of-Thought/Assets/Scripts/PlayerWeaponController.cs
Gerard Gascón 862afc9b7a init
2025-04-24 16:56:52 +02:00

44 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerWeaponController : MonoBehaviour
{
private GameObject weapon;
public RawImage bulletBar;
public float bullets = 11f;
[HideInInspector]
public float currentBullets;
// Start is called before the first frame update
void Start()
{
currentBullets = bullets;
}
// Update is called once per frame
void Update()
{
currentBullets = Mathf.Clamp(currentBullets, 0, bullets);
if (Inventory.Items.Count > 0) {
if (weapon==null || weapon.name != Inventory.getActiveItem().name) {
if (weapon != null) {
Object.Destroy(weapon);
}
weapon = Instantiate(Inventory.getActiveItem());
weapon.name = Inventory.getActiveItem().name;
weapon.transform.SetParent(gameObject.transform);
weapon.transform.position = gameObject.transform.position;
weapon.transform.localScale = new Vector3(2.5f, 2.5f, 2.5f);
Destroy(weapon.GetComponent<BoxCollider2D>());
Destroy(weapon.GetComponent<Rigidbody2D>());
}
}
bulletBar.transform.localScale = new Vector2(currentBullets, bulletBar.transform.localScale.y);
if (Input.GetKeyDown(KeyCode.Mouse0) && currentBullets > 0) {
weapon.GetComponent<Weapon>().shoot();
currentBullets--;
}
}
}