-2

I'm attempting to generate random dungeons using prefabs.

I have it working to where it will pick a random doorway from a list of doorways and spawn a new room. I then have a script on the bounding box to update the room script for when it intersects another room.

The issue that I am currently having is that I'm not entirely sure on how to wait or delay to check and see if the onTriggerEvent has been called.

I'm currently checking to see if there was a collision, and setting a boolean variable to true when that happens otherwise it's false. This always results with the variable being false even though there is collision occurring.

Edit:
This is currently how i'm checking to see if a room is valid:

DungeonGenerator.cs:

// This is being ran in a for loop.
if (currentRoom.isValid())
{
   // Removed for clarity. This essentially just places a room.
}

RoomValidation.cs:

private void OnTriggerEnter(Collider other)
{
    // This is the variable i was talking about.
    // Currently it seems like this is always false when i check.
    // If it's false then it fails validation.
    if (other.tag.equals("Room"))
          room.validated.Invoke(false);
}

The isValid() function just returns a validated boolean variable. This doesn't seem to update fast enough for the collision to update. I've looked into Coroutine's and that doesn't seem feasible to implement. Wouldn't that just run outside of the loop and cause the same issue?

Extain
  • 72
  • 7
  • Please add your relevant code – derHugo Sep 20 '21 at 06:31
  • Does this answer your question: [How to make the script wait/sleep in a simple way in unity](https://stackoverflow.com/questions/30056471/how-to-make-the-script-wait-sleep-in-a-simple-way-in-unity) – derHugo Sep 20 '21 at 06:33
  • @derHugo i have updates the post with the code. – Extain Sep 21 '21 at 01:19
  • I'm a little bit confused ... `other.tag.equals("Room")` wouldn't even compile .. in general instead of comparing the strings rather use `other.CompareTag("Room")` which rather compares Hashes. And then your code doesn't explain much .. how are the two related? `OnTriggerEnter` is called physics based so earliest in the next `FixedUpdate` step except you simulate the physics manually ... – derHugo Sep 21 '21 at 04:23
  • I'll give that a try, I've never had any issues using other.tag.equals. I understand that it gets called on the physics update, which is why i'm concerned that the check is passing faster than it can properly check. So, the onTriggerEnter runs on the bounding box i have created for the room, and that updates the Room script and tells it if it's validated or not. Then, the DungeonGenerator script checks the room to see if it's valid. If that makes sense. – Extain Sep 22 '21 at 00:50
  • @derHugo, I've tried using what you have said, it's still showing rooms on-top of each other, every so often it will actually fail validation and try to place a new room. I'm now running this inside of a Coroutine, and I have a `yield return new WaitForFixedUpdate()` running right before I check for room validation. Using this it still tends to place rooms on-top of each other. I'll provide the code once I'm off work. – Extain Sep 23 '21 at 23:34

2 Answers2

0

You can use OnTriggerStay() if I understand correctly, as it returns true if there is something in the trigger and false otherwise. If you are using 2d, use OnTriggerStay2D(). To use it, just define it as a function like you would normally, with an argument of Collider other, with Collider being the type, and other being the other collider.. Then, you can check details about the collider(for example you can check if it's the player) and then do stuff to the other collider(and by extension, the entire object) and the object the script is attached to. Here is an example, but it may require tweaking to perfectly fit what you want to do.

public class YourClass
{
    void OnTriggerStay(other) //OnTriggerStay2D if it's 2D
    {
        if (other.tag == "player") //Checks by tag, only works if the player has a
        //player tag, and if it doesn't... well, it probably should.
        {
            UpdateRoom(); //Update room, idk how you would do it but replace this when you do it
        }
    }
}

If there's anything I missed, just reply and I'll do my best to help.

GabRio Blu
  • 85
  • 10
  • It's not that it's not resulting in a collision, It's not waiting to see if that collision does happen, if that makes sense. I'm currently running this in a for loop that checks if the room is valid. I have updated my original post to resemble the code i am running. – Extain Sep 21 '21 at 01:19
  • @Extain as I said, try replacing `OnTriggerEnter()` with `OnTriggerStay()` and tell me how it goes. Also, are you using 2d or 3d? – GabRio Blu Sep 22 '21 at 12:07
  • I gave it a try, I'm working with 3D. Even with that it's still producing the same results. – Extain Sep 22 '21 at 15:41
0

So, I have finally got it working. I changed how the place rooms function operates. Instead of checking if the room is valid in the function I now do it outside of it. I place the room, then wait for the fixed update to run and then check to see if it's valid. Don't know why this wasn't working the other way before. I am able to generate dungeons now without any issues.

Extain
  • 72
  • 7