Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RocketSpawner : MonoBehaviour
  6. {
  7. [SerializeField] private List<Transform> rocketPositions;
  8. [SerializeField] private GameObject rocketPrefab;
  9.  
  10. private void Update()
  11. {
  12. if (Input.GetKeyDown (KeyCode.A))
  13. SpawnRocket ();
  14. }
  15.  
  16. /// Spawn a rocket.
  17. /// It is a public void so you can call it from any other script.
  18. public void SpawnRocket ()
  19. {
  20. if (rocketPrefab != null)
  21. {
  22. Vector3 randomPosition = GetRandomPosition ();
  23. GameObject newRocket = Instantiate (rocketPrefab, randomPosition, Quaternion.identity);
  24. }
  25. else
  26. {
  27. Debug.LogWarning ("You didn't assign the rocket prefab in the inspector.");
  28. }
  29. }
  30.  
  31. /// Get a random position
  32. private Vector3 GetRandomPosition ()
  33. {
  34. if (rocketPositions.Count > 0)
  35. {
  36. //Get a random position from our rocket positions list assigned in the editor.
  37. return rocketPositions [Random.Range (0, rocketPositions.Count)].position;
  38. } else
  39. {
  40. Debug.LogWarning ("You didn't set the rocket positions in the inspector. We will return Vector3.zero");
  41. return Vector3.zero;
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement