Here's a script to accomplish what you need in Unity using C#. This script will handle spawning houses inside trigger box colliders, ensuring they are placed on the ground, and spawning a special prefab near each house with appropriate gizmos.
using System.Collections.Generic;
using UnityEngine;
public class HouseSpawner : MonoBehaviour
{
public List<BoxCollider> boxColliders = new List<BoxCollider>();
public List<GameObject> housePrefabs = new List<GameObject>();
public GameObject specialPrefab;
public LayerMask groundLayer;
private List<GameObject> spawnedHouses = new List<GameObject>();
private List<GameObject> spawnedSpecials = new List<GameObject>();
void Start()
{
SpawnHouses();
}
void SpawnHouses()
{
foreach (BoxCollider box in boxColliders)
{
Vector3 randomPoint;
if (GetRandomPointOnGround(box, out randomPoint))
{
GameObject house = Instantiate(housePrefabs[Random.Range(0, housePrefabs.Count)], randomPoint, Quaternion.identity);
spawnedHouses.Add(house);
Vector3 randomOffset = Random.insideUnitSphere;
randomOffset.y = 0; // Keep it on the same level horizontally
randomOffset = randomOffset.normalized * Random.Range(10, 50);
Vector3 specialSpawnPoint = randomPoint + randomOffset;
GameObject special = Instantiate(specialPrefab, specialSpawnPoint, Quaternion.identity);
spawnedSpecials.Add(special);
}
}
}
bool GetRandomPointOnGround(BoxCollider box, out Vector3 point)
{
float rangeX = box.size.x / 2;
float rangeZ = box.size.z / 2;
for (int attempt = 0; attempt < 10; attempt++)
{
point = new Vector3(
Random.Range(-rangeX, rangeX),
0,
Random.Range(-rangeZ, rangeZ)
);
point = box.transform.TransformPoint(point);
if (Physics.Raycast(point + Vector3.up * 100, Vector3.down, out RaycastHit hit, Mathf.Infinity, groundLayer))
{
point = hit.point;
return true;
}
}
point = Vector3.zero;
return false;
}
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
foreach (GameObject house in spawnedHouses)
{
if (house != null)
{
Gizmos.DrawSphere(house.transform.position, 1f);
}
}
Gizmos.color = Color.red;
foreach (GameObject special in spawnedSpecials)
{
if (special != null)
{
Gizmos.DrawLine(special.transform.position, special.transform.position + Vector3.up * 100);
}
}
}
}
Explanation:
- boxColliders: List of box colliders where houses can spawn.
- housePrefabs: List of house prefabs to instantiate.
- specialPrefab: Special prefab to spawn near the house.
- groundLayer: Layer mask to check for ground.
Steps:
- SpawnHouses(): Iterates through each box collider. For each collider, it tries to get a random point on the ground inside the collider.
- GetRandomPointOnGround(): Attempts to find a valid point on the ground within the given BoxCollider.
- Spawns the house and then a special prefab near it, ensuring the special prefab is within a random distance of 10 to 50 units.
- OnDrawGizmos(): Draws gizmos for the instantiated houses and special prefabs.
Make sure to:
- Assign the
boxColliders list with your box colliders.
- Assign the
housePrefabs list with your house prefabs.
- Assign the
specialPrefab with your special prefab.
- Set the
groundLayer to the appropriate layer in your Unity project.
Attach this script to an empty GameObject in your scene and configure the public variables through the Inspector. When you run the scene, the script will spawn houses and special prefabs as described.