Advertisement
lemansky

Untitled

Apr 8th, 2025
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Blaster : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.     public GameObject bulletPrefab;
  9.     public float fireRate = 0.2f;
  10.     public float fireCooldown = -1.0f;
  11.     public AudioClip bulletClip;
  12.     AudioSource audioSource;
  13.     void Start()
  14.     {
  15.         audioSource = GameObject.Find("BulletSound").GetComponent<AudioSource>();
  16.     }
  17.     void Update()
  18.     {
  19.         if (Input.GetMouseButtonDown(0) && Time.time > fireCooldown)
  20.         {
  21.             fireCooldown = Time.time + fireRate;
  22.             GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
  23.             audioSource.PlayOneShot(bulletClip);
  24.             StartCoroutine(DestroyDelay(bullet));
  25.         }
  26.     }
  27.  
  28.     IEnumerator DestroyDelay(GameObject bullet)
  29.     {
  30.         yield return new WaitForSeconds(5.0f);
  31.         Destroy(bullet);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement