lemansky

Untitled

Apr 8th, 2025 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 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.         audioSource.clip = bulletClip;
  17.     }
  18.     void Update()
  19.     {
  20.         if (Input.GetMouseButtonDown(0) && Time.time > fireCooldown)
  21.         {
  22.             fireCooldown = Time.time + fireRate;
  23.             GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
  24.             audioSource.Play();
  25.             StartCoroutine(DestroyDelay(bullet));
  26.         }
  27.     }
  28.  
  29.     IEnumerator DestroyDelay(GameObject bullet)
  30.     {
  31.         yield return new WaitForSeconds(5.0f);
  32.         Destroy(bullet);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment