Procedural Level Generator Script – Unity

using UnityEngine;
using System.Collections.Generic;

public class LevelGenerator : MonoBehaviour {

    public GameObject[] obstacles;  // array of obstacle prefabs
    public GameObject[] enemies;  // array of enemy prefabs
    public int minDistance = 10;  // minimum distance between objects
    public int maxDistance = 20;  // maximum distance between objects
    public int numObstacles = 10;  // number of obstacles to place
    public int numEnemies = 5;  // number of enemies to place

    void Start () {
        // randomly place obstacles
        for (int i = 0; i < numObstacles; i++) {
            // choose a random obstacle prefab
            int randIndex = Random.Range(0, obstacles.Length);
            GameObject obstacle = obstacles[randIndex];
            
            // choose a random position and rotation
            Vector3 position = GetRandomPosition();
            Quaternion rotation = GetRandomRotation();
            
            // check if the position is valid (i.e. not too close to other objects)
            if (IsValidPosition(position)) {
                // instantiate the obstacle at the position and rotation
                Instantiate(obstacle, position, rotation);
            }
        }

        // randomly place enemies
        for (int i = 0; i < numEnemies; i++) {
            // choose a random enemy prefab
            int randIndex = Random.Range(0, enemies.Length);
            GameObject enemy = enemies[randIndex];
            
            // choose a random position and rotation
            Vector3 position = GetRandomPosition();
            Quaternion rotation = GetRandomRotation();
            
            // check if the position is valid (i.e. not too close to other objects)
            if (IsValidPosition(position)) {
                // instantiate the enemy at the position and rotation
                Instantiate(enemy, position, rotation);
            }
        }
    }

    // helper functions to get random positions and rotations
    Vector3 GetRandomPosition () {
        float x = Random.Range(-50f, 50f);
        float z = Random.Range(-50f, 50f);
        return new Vector3(x, 0f, z);
    }
    
    Quaternion GetRandomRotation () {
        return Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
    }

    // helper function to check if a position is valid (i.e. not too close to other objects)
    bool IsValidPosition (Vector3 position) {
        Collider[] hitColliders = Physics.OverlapSphere(position, minDistance);
        if (hitColliders.Length > 0) {
            return false;
        } else {
            return true;
        }
    }
}

This code creates a Level Generator script that randomly places obstacles and enemies in the scene. The obstacles and enemies arrays contain the prefabs for the objects to place, and the minDistance and maxDistance variables define the minimum and maximum distances between objects.

The Start() function uses two for loops to place a certain number of obstacles and enemies. The GetRandomPosition() and GetRandomRotation() helper functions generate random positions and rotations for the objects to be placed. The IsValidPosition() helper function checks if a position is valid (i.e. not too close to other objects) before instantiating the object at that position and rotation.

Overall, this script generates a random level by randomly placing obstacles and enemies throughout the scene, creating a unique and varied experience for each playthrough.

for more;unity asset collection

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock