Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 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. private void SpawnRocket ()
  18. {
  19. if (rocketPrefab != null)
  20. {
  21. Vector3 randomPosition = GetRandomPosition ();
  22. GameObject newRocket = Instantiate (rocketPrefab, randomPosition, Quaternion.identity);
  23. }
  24. else
  25. {
  26. Debug.LogWarning ("You didn't assign the rocket prefab in the inspector.");
  27. }
  28. }
  29.  
  30. /// Get a random position
  31. private Vector3 GetRandomPosition ()
  32. {
  33. if (rocketPositions.Count > 0)
  34. {
  35. //Get a random position from our rocket positions list assigned in the editor.
  36. return rocketPositions [Random.Range (0, rocketPositions.Count)].position;
  37. } else
  38. {
  39. Debug.LogWarning ("You didn't set the rocket positions in the inspector. We will return Vector3.zero");
  40. return Vector3.zero;
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement