Certainly! Below is a Unity C# script that implements your requirements. The script performs the random selection of spawn points, spawns houses, places a special prefab object some distance away from each house, and sets up gizmos for visualization.
Assumptions:
- The box colliders are assigned as "Trigger Boxes" in the script.
- You have a set of house prefabs and a special prefab.
- The script manages spawning and visualization using gizmos.
Here is the script:
using System.Collections.Generic;
using UnityEngine;
public class HouseSpawner : MonoBehaviour
{
public List<Transform> spawnPoints;
public List<GameObject> triggerBoxes;
public List<GameObject> housePrefabs;
public GameObject specialPrefab;
private List<GameObject> spawnedHouses = new List<GameObject>();
private List<GameObject> spawnedSpecials = new List<GameObject>();
private void Start()
{
SpawnHouses();
}
private void SpawnHouses()
{
foreach (Transform spawnPoint in spawnPoints)
{
// Randomly select a trigger box that contains the spawn point
GameObject randomTriggerBox = triggerBoxes[Random.Range(0, triggerBoxes.Count)];
if (randomTriggerBox.GetComponent<Collider>().bounds.Contains(spawnPoint.position))
{
// Perform raycast from up to down to find the exact placement position
RaycastHit hit;
Vector3 rayOrigin = spawnPoint.position + Vector3.up * 100; // assuming 100 is high enough
if (Physics.Raycast(rayOrigin, Vector3.down, out hit))
{
// Spawn the house
GameObject house = Instantiate(housePrefabs[Random.Range(0, housePrefabs.Count)], hit.point, Quaternion.identity);
spawnedHouses.Add(house);
// Spawn special prefab near the house
Vector3 specialPosition = GenerateRandomPositionAround(hit.point, 10, 50);
GameObject special = Instantiate(specialPrefab, specialPosition, Quaternion.identity);
spawnedSpecials.Add(special);
// Use particle system on house spawn if needed
ParticleSystem ps = house.GetComponent<ParticleSystem>();
if (ps != null)
{
ps.Play();
}
}
}
}
}
private Vector3 GenerateRandomPositionAround(Vector3 center, float minDistance, float maxDistance)
{
float distance = Random.Range(minDistance, maxDistance);
float angle = Random.Range(0, 360);
Vector3 offset = new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), 0, Mathf.Cos(angle * Mathf.Deg2Rad)) * distance;
return center + offset;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
foreach (GameObject house in spawnedHouses)
{
Gizmos.DrawWireSphere(house.transform.position, 1);
}
Gizmos.color = Color.blue;
foreach (GameObject special in spawnedSpecials)
{
Gizmos.DrawWireCube(special.transform.position, Vector3.one);
Gizmos.DrawLine(special.transform.position, special.transform.position + Vector3.up * 100);
}
}
}
How to Set Up the Script
- Attach the
HouseSpawner script to an empty GameObject in the scene.
- Assign the
spawnPoints list:
- Add all potential spawn point Transforms into this list.
- Assign the
triggerBoxes list:
- Add the GameObjects containing the Trigger Box colliders here.
- Assign the
housePrefabs list:
- Add various house prefabs to this list.
- Assign the
specialPrefab:
- Add the special prefab to this field.
Explanation
- Selecting Random Points: The script selects a random point contained within one of the trigger boxes.
- Raycast for Spawning: The script casts a ray from a height above the spawn point downward to determine the ground position for spawning the house.
- Spawning the Special Prefab: After placing the house, another object (the special prefab) is spawned at a random distance between 10 to 50 meters from the house.
- Particle System Trigger: The script checks if the spawned house has a particle system and plays it if applicable.
- Gizmos: The script draws gizmos (green spheres for houses and blue cubes with lines for special prefabs) to visually debug and verify positions.
Ensure that this script is functioning as intended within your specific project context, since some adjustments might be needed based on the specific configurations and requirements of your game/scene.