Skip to content

Instantly share code, notes, and snippets.

@AG-Dan
Created July 30, 2019 19:25
Show Gist options
  • Save AG-Dan/d6c220861f471389aa5373981b19373a to your computer and use it in GitHub Desktop.
Save AG-Dan/d6c220861f471389aa5373981b19373a to your computer and use it in GitHub Desktop.
using DunGen;
using UnityEngine;
[RequireComponent(typeof(RuntimeDungeon))]
class PlayerSpawner : MonoBehaviour
{
public GameObject PlayerPrefab = null;
private GameObject currentPlayer;
private void Awake()
{
var runtimeDungeon = GetComponent<RuntimeDungeon>();
runtimeDungeon.Generator.OnGenerationStatusChanged += OnDungeonGenerationStatusChanged;
}
private void OnDungeonGenerationStatusChanged(DungeonGenerator generator, GenerationStatus status)
{
if (status == GenerationStatus.Complete)
SpawnPlayer();
}
private void SpawnPlayer()
{
// Destroy the current player if one exists
if (currentPlayer != null)
Destroy(currentPlayer);
// Instantiate a new player instance
currentPlayer = Instantiate(PlayerPrefab);
// Find a random GameObject with the "Respawn" tag
var potentialSpawnPoints = GameObject.FindGameObjectsWithTag("Respawn");
var chosenSpawnPoint = potentialSpawnPoints[Random.Range(0, potentialSpawnPoints.Length)];
// Teleport the newly instantiated player to the chosen spawn point
currentPlayer.transform.position = chosenSpawnPoint.transform.position;
currentPlayer.transform.rotation = chosenSpawnPoint.transform.rotation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment