script 1:
using UnityEngine;
public class test_trigger : MonoBehaviour
{
// Et referansepunkt for å spawne objekter
public GameObject objectToSpawn;
public GameObject turn1;
public GameObject turn2;
public GameObject turn3;
public GameObject turn4;
public GameObject corridor1;
public GameObject corridor2;
public GameObject threeway1;
public GameObject threeway2;
public GameObject threeway3;
public GameObject threeway4;
public GameObject fourway;
public int rand_num = 0;
public Vector2[] vectorArray = new Vector2[6];
private int currentPositionIndex = 0;
void Start()
{
// Legg til startposisjon i vectorArray
AddPosition(Vector2.zero);
a(); // Kall a() i start
}
public void AddPosition(Vector2 position)
{
if (currentPositionIndex < vectorArray.Length)
{
vectorArray[currentPositionIndex] = position;
currentPositionIndex++;
}
else
{
Debug.LogWarning("Vector array is full. Cannot add more positions.");
}
}
public bool PositionExists(Vector2 position)
{
foreach (Vector2 pos in vectorArray)
{
if (pos.Equals(position))
{
return true;
}
}
return false;
}
public void a()
{
// Mulighet for å endre spawn-sjansen
rand_num = UnityEngine.Random.Range(30, 81);
if(rand_num <= 10)
{
Debug.Log("turn1");
objectToSpawn = turn1;
}
else if(rand_num <= 20)
{
Debug.Log("turn2");
objectToSpawn = turn2;
}
else if(rand_num <= 30)
{
Debug.Log("turn3");
objectToSpawn = turn3;
}
else if(rand_num <= 40)
{
Debug.Log("turn4");
objectToSpawn = turn4;
}
else if(rand_num <= 50)
{
Debug.Log("fourway");
objectToSpawn = fourway;
}
else if(rand_num <= 60)
{
Debug.Log("corridor1");
objectToSpawn = corridor1;
}
else if(rand_num <= 70)
{
Debug.Log("corridor2");
objectToSpawn = corridor2;
}
else if(rand_num <= 80)
{
Debug.Log("threeway");
objectToSpawn = threeway1;
}
}
}
script 2:
using UnityEngine;
public class trigger3 : MonoBehaviour
{
// Reference to test_trigger instance
public test_trigger logic;
private bool roomSpawned = false; // Markerer om rommet allerede er blitt spawneta
private void Start()
{
// Find and assign the test_trigger instance
logic = GameObject.FindWithTag("Finish").GetComponent<test_trigger>();
}
private void OnTriggerEnter2D(Collider2D other)
{
// Check if logic is assigned and if the room has not been spawned yet
if (logic != null && !roomSpawned)
{
// Call spawn method on the assigned test_trigger instance
Vector2 spawnPosition = new Vector2(transform.position.x + -17f, transform.position.y);
// Check if spawnPosition is not (0, 0)
if (spawnPosition != Vector2.zero)
{
// Check if spawnPosition collides with existing positions in vectorArray
if (!logic.PositionExists(spawnPosition))
{
logic.a();
if(!roomSpawned)
{Instantiate(logic.objectToSpawn, spawnPosition, Quaternion.identity);
// Add the spawned position to vectorArray
logic.AddPosition(spawnPosition);
roomSpawned = true; }
}
else
{
Debug.Log("Position already exists. Skipping spawn.");
}
}
else
{
Debug.Log("Spawn position is (0, 0). Skipping spawn.");
}
}
else
{
Debug.LogError("test_trigger is not assigned or room has already been spawned.");
}
}
}