using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class PlayerGun : MonoBehaviour { public float offset; // Rotation offset for the gun public GameObject bullet; // Bullet prefab public Transform firePoint; // Point from which bullets are fired private float timeBetweenShots; // Time between shots public float startTimeBetweenShots; // Initial time between shots public int gunFireStaminaCost = 60; // Stamina cost per shot public PlayerStamina playerStamina; // Reference to player stamina AudioManager audioManager; // Reference to the audio manager void Awake() { // Initialize audio manager audioManager = GameObject.FindGameObjectWithTag("Audio").GetComponent(); } void Start() { // Initialize player stamina if not assigned if (playerStamina == null) { playerStamina = GetComponentInParent(); } } void Update() { // Rotate gun towards the mouse Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; float rotateZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotateZ + offset); // Handle shooting if (timeBetweenShots <= 0) { if (Input.GetMouseButtonDown(1) && playerStamina.UseStamina(gunFireStaminaCost)) { audioManager.PlaySFX(audioManager.bulletAttackSFX); Instantiate(bullet, firePoint.position, transform.rotation); timeBetweenShots = startTimeBetweenShots; // Reset shot timer } } else { timeBetweenShots -= Time.deltaTime; // Countdown to next shot } } }