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 RocketSpawner : MonoBehaviour
- {
- [SerializeField] private List<Transform> rocketPositions;
- [SerializeField] private GameObject rocketPrefab;
- private void Update()
- {
- if (Input.GetKeyDown (KeyCode.A))
- SpawnRocket ();
- }
- /// Spawn a rocket.
- private void SpawnRocket ()
- {
- if (rocketPrefab != null)
- {
- Vector3 randomPosition = GetRandomPosition ();
- GameObject newRocket = Instantiate (rocketPrefab, randomPosition, Quaternion.identity);
- }
- else
- {
- Debug.LogWarning ("You didn't assign the rocket prefab in the inspector.");
- }
- }
- /// Get a random position
- private Vector3 GetRandomPosition ()
- {
- if (rocketPositions.Count > 0)
- {
- //Get a random position from our rocket positions list assigned in the editor.
- return rocketPositions [Random.Range (0, rocketPositions.Count)].position;
- } else
- {
- Debug.LogWarning ("You didn't set the rocket positions in the inspector. We will return Vector3.zero");
- return Vector3.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement