Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spawn player at 1st room

Discussion in 'Scripting' started by Paykoman, Nov 6, 2020.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys,

    i have this little level generator that works simple but efficiente, but i would like to ask help for one thing, how can i define the room as start point for player spawn and last room has exist room? since i can only spawn my room prefabs randomly but i dont know how to set up a inicial and an end room specificaly so the dungeon just run between this 2 rooms.

    There is my scripts. Appreciate
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class LevelGeneration : MonoBehaviour
    7. {
    8.     public Transform[] startingPos;
    9.     public GameObject[] rooms; // index 0 --> LR, indes 1 --> LRB, index 2 --> LRT, index 3 --> LRBT
    10.  
    11.     private int direction;
    12.     public float moveAmount;
    13.  
    14.     private float timeBtwRoom;
    15.     public float startTimeBtwRoom = 0.25f;
    16.  
    17.     public float minX;
    18.     public float maxX;
    19.     public float minY;
    20.  
    21.     public bool stopGeneration;
    22.  
    23.     public LayerMask room;
    24.  
    25.     private int downCounter;
    26.  
    27.     void Start()
    28.     {
    29.         int ranStartingPos = Random.Range(0, startingPos.Length);
    30.         transform.position = startingPos[ranStartingPos].position;
    31.         Instantiate(rooms[0], transform.position, Quaternion.identity);
    32.  
    33.         direction = Random.Range(1, 6);
    34.     }
    35.  
    36.     private void Update()
    37.     {
    38.         if(timeBtwRoom <= 0 && stopGeneration == false)
    39.         {
    40.             Move();
    41.             timeBtwRoom -= startTimeBtwRoom;
    42.         }
    43.         else
    44.         {
    45.             timeBtwRoom -= Time.deltaTime;
    46.         }
    47.     }
    48.  
    49.     private void Move()
    50.     {
    51.         if(direction == 1 || direction == 2) //Move Right!
    52.         {
    53.             if (transform.position.x < maxX)
    54.             {
    55.                 downCounter = 0;
    56.                 Vector2 newPos = new Vector2(transform.position.x + moveAmount, transform.position.y);
    57.                 transform.position = newPos;
    58.  
    59.                 int rand = Random.Range(0, rooms.Length);
    60.                 Instantiate(rooms[rand], transform.position, Quaternion.identity);
    61.  
    62.                 direction = Random.Range(1, 6);
    63.                 if(direction == 3)
    64.                 {
    65.                     direction = 2;
    66.                 } else if(direction == 4)
    67.                 {
    68.                     direction = 5;
    69.                 }
    70.             }
    71.             else
    72.             {
    73.                 direction = 5;
    74.             }  
    75.            
    76.         } else if(direction == 3 || direction == 4) //Move Left!
    77.         {
    78.             if (transform.position.x > minX)
    79.             {
    80.                 downCounter = 0;
    81.                 Vector2 newPos = new Vector2(transform.position.x - moveAmount, transform.position.y);
    82.                 transform.position = newPos;
    83.  
    84.                 int rand = Random.Range(0, rooms.Length);
    85.                 Instantiate(rooms[rand], transform.position, Quaternion.identity);
    86.  
    87.                 direction = Random.Range(3, 6);
    88.             }
    89.             else
    90.             {
    91.                 direction = 5;
    92.             }
    93.            
    94.         } else if (direction == 5) //Move down!
    95.         {
    96.             downCounter++;
    97.  
    98.             if(transform.position.y > minY)
    99.             {
    100.                 Collider2D roomDetection = Physics2D.OverlapCircle(transform.position, 1, room);
    101.                 if(roomDetection.GetComponent<RoomType>().type !=1 && roomDetection.GetComponent<RoomType>().type != 3)
    102.                 {
    103.                     if(downCounter >= 2)
    104.                     {
    105.                         roomDetection.GetComponent<RoomType>().RoomDestruction();
    106.                         Instantiate(rooms[3], transform.position, Quaternion.identity);
    107.                     }
    108.                     else
    109.                     {
    110.                         roomDetection.GetComponent<RoomType>().RoomDestruction();
    111.  
    112.                         int randBottomRoom = Random.Range(1, 4);
    113.                         if (randBottomRoom == 2)
    114.                         {
    115.                             randBottomRoom = 1;
    116.                         }
    117.                         Instantiate(rooms[randBottomRoom], transform.position, Quaternion.identity);
    118.                     }                  
    119.                 }
    120.  
    121.                 Vector2 newPos = new Vector2(transform.position.x, transform.position.y - moveAmount);
    122.                 transform.position = newPos;
    123.  
    124.                 int rand = Random.Range(2, 4);
    125.                 Instantiate(rooms[rand], transform.position, Quaternion.identity);
    126.  
    127.                 direction = Random.Range(1, 6);
    128.             }
    129.             else
    130.             {
    131.                 //Stop Level Generation!
    132.                 stopGeneration = true;
    133.             }
    134.         }
    135.     }
    136. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnRoom : MonoBehaviour
    6. {
    7.     public LayerMask whatIsRoom;
    8.     public LevelGeneration levelGen;
    9.  
    10.     void Update()
    11.     {
    12.         Collider2D roomDetection = Physics2D.OverlapCircle(transform.position, 1, whatIsRoom);
    13.         if (roomDetection == null && levelGen.stopGeneration == true)
    14.         {
    15.             //Spaw random room
    16.             int rand = Random.Range(0, levelGen.rooms.Length);
    17.             Instantiate(levelGen.rooms[rand], transform.position, Quaternion.identity);
    18.             Destroy(gameObject);
    19.         }
    20.     }
    21. }
    22.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    One approach is that when you are making the rooms you would insert a GameObject into the scene where the first room is and mark it as the start, and then do the same for the exit.

    Then your player code would look for the start to spawn, and compare to being close enough to the exit to exit.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The question is, i have four rooms that spawn randomly, so if i put a game object in one room, that room could spawn multiple times, so it will conflict right? Or i can do something like if player not equal to null dont spawn a second time, since i spawn the rooms one after another with .25s delay.. i really dont get it properly.. im a bit new at this :p
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The
    Instantiate<GameObject>(rooms[0] ...)
    construct returns to you a GameObject that is the top of the thing it made.

    One strategy is to put a dummy "start" and a dummy "exit" GameObject in every one of your rooms, and AFTER you instantiate it, dig through the hierarchy to destroy the dummies when you don't want them, eg, for start it would be any of your subsequent rooms, and for end it would be all but the last room.

    You can make an empty MonoBehavior to identify starts and exits too, which makes it really easy to find using the .GetComponentInChildren() call, which you would do upon the instantiated room return.

    I like this pattern:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class IdentifyStart : MonoBehaviour
    4. {
    5.     // does nothing, just marks a gameobject as a start point
    6. }
    Then you can also have your player search the entire scene for that (using
    FindObjectOfType<IdentifyStart>()
    ) to figure out where to spawn.
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I can understand what ur say but pass it to code isnt easy some times, even when its seems and is pretty simple.. but appreciate your time m8.
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    hi again guys, after some work i get a new problem... every room i made have a collider 2d at the center of the room for generation purposes, but the problem is after level generate since the collider keeps in the game my character stucks at it... i would like to know if i can disable the collider after the map is generated so the player wouldn't collide with it.

    Appreciate, codes above and picture showing the problem.

     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Yes you can. Set its .enabled property to false or else Destroy() the collider.
     
  8. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I try .enable = false, but them the map generated 2 or 3 rooms and stop. Where should i out it in code? Maybe im putting in in wrong place..