Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Blaster : MonoBehaviour
- {
- // Start is called before the first frame update
- public GameObject bulletPrefab;
- public float fireRate = 0.2f;
- public float fireCooldown = -1.0f;
- public AudioClip bulletClip;
- AudioSource audioSource;
- void Start()
- {
- audioSource = GameObject.Find("BulletSound").GetComponent<AudioSource>();
- }
- void Update()
- {
- if (Input.GetMouseButtonDown(0) && Time.time > fireCooldown)
- {
- fireCooldown = Time.time + fireRate;
- GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
- audioSource.PlayOneShot(bulletClip);
- StartCoroutine(DestroyDelay(bullet));
- }
- }
- IEnumerator DestroyDelay(GameObject bullet)
- {
- yield return new WaitForSeconds(5.0f);
- Destroy(bullet);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement