Advertisement
ChrisTutorials

ProjectileSpawer Fixed

Feb 28th, 2023
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ProjectileSpawner : MonoBehaviour
  6. {
  7.     // Projectile to Spawn
  8.     public GameObject projectile;
  9.  
  10.     // Where to Spawn the Projectiles
  11.     public Transform spawnLocation;
  12.  
  13.     // Rotation of Projectile on Spawn
  14.     public Quaternion spawnRotation;
  15.  
  16.     public DetectionZone detectionZone;
  17.     public AudioSource spawnAudioSource;
  18.  
  19.     public float spawnTime = 0.5f;
  20.  
  21.     private float timeSinceSpawned = 0.5f;
  22.  
  23.     // Update is called once per frame
  24.     private void Update()
  25.     {
  26.         if (detectionZone.detectedObjs.Count > 0)
  27.         {
  28.             timeSinceSpawned += Time.deltaTime;
  29.  
  30.             if (timeSinceSpawned >= spawnTime)
  31.             {
  32.                 Instantiate(projectile, spawnLocation.position, spawnRotation);
  33.  
  34.                 if (spawnAudioSource.clip)
  35.                 {
  36.                     spawnAudioSource.Play();
  37.                 }
  38.  
  39.                 timeSinceSpawned = 0;
  40.             }
  41.         }
  42.         else
  43.         {
  44.             timeSinceSpawned = 0.5f;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement