Any expert can help me with this, please?

Hi there. I already searched a lot around the forums, and wiki… and all… with no luck.

The thing is:
I have a room and i need a trigger to fire some events (with playmaker, i change music and such); but i cant use a box trigger because the shape of the rooom. If i use that, the trigger has not the same shape as the room and intersect other rooms.
I cant use mesh collider as trigger, because it does not let me to make the collider “trigger”. And if i make it Convex, i can… but again it does not match the room shape.

Sooo after trying multiple things and reading a lot, i tryed to use compound triggers and t think thats the way.
But again, i dont know how to work with that. Its just not working.
I have a object with a rigid body and 3 children below each one with a trigger.
Then i tried everything (the main object with the script wich fires the events… or giving the script to the children… with no luck.

The thing is that i need “on enter” and “on exit triggers”, because the events happens while in the room, and it reverts back to the initial state when i get out.
So having those multiple colliders make it so i enter the first collider and the event starts (no compound needed for that), but as i enter de next trigger i am already exiting the first one so the event finish off… and i want to have it while im in the entire room…

I dont know i you can understand me lol…
See screenshot

How i can do this, please?

How are you creating the rooms? What I would do is just create a simple mesh with the correct shape of the room and set that as the meshcollider. Then the collider will be the same shape as the room. edit: I just learned more about what a convex shape is. Above is mistakenly incorrect.

An alternative is to place seperate triggers in each door and keep track of whether the player is in the room by using a custom static class with a boolean member which you can toggle in the triggers.

Hi there and thanks for your time!!

About the first thing, i lready made that. Made a simple mesh in an external program with the shape of the room. but its still the same… Its mesh collider in Unity canT be set to “trigger”. Only if i set the collider to convex… wich leads to the collider changing its shape, not getting the same as the mesh…

The other way you say, yeah that way it could work, but its a lot of work because Its not only that on enter play do this or on exit do that. I must take into account the doors, as the player not only can go one way (into the room); he also can go in trigger and then turn back to the exit…
or he can go into the room and when he leaves one of the triggers to go into other, hes not leaving the room… but he is activating the on trigger exit…(and then he goes into the new trigger); as i explain in the screenshot in the first post.


Blue is the door:
Red is the Trigger in the room.
Yellow is the Trigger outside of the room.

You can now in a room script keep track of the door GOs:public GameObject[] doors;
And you can keep track of whether the player is in the Room: public bool playerInRoom;

When the player triggers the red trigger you check whether ‘playerInRoom’ is true or not and if not you execute the logic needed when the player enters the room. Same goes for when the player exits the room.

Now, when the player enters the room and triggers and when he exits the room, he will trigger the exit logic. Also this way he can not activate the exit logic without leaving. You could go further by building in delays so when a payer quickly enter and then exits the enter logic is not executed and vice versa.

Usually a game checks if the player is contained within some shape (the outline of the room) after colliding with a trigger that is the size and shape of a doorway.

This is also why rooms tend to be rectangular (or have rectangular positioned doors) in most games, because that “contains” check just involves checking the bounding box, which is roughly this:

public static bool IsPlayerInRoom (GameObject playerGameObject, GameObject roomGameObject) {

  // Player's position:
  Vector3 playerPosition = playerGameObject.transform.position;

  // Room position:
  Vector3 roomPosition = roomGameObject.transform.position;

  // Player relative to the room:
  Vector3 playerInRoom = roomPosition - playerPosition;

  // Now for the magic!

  // Get the room mesh:
  Mesh roomMesh = roomGameObject.GetComponent<MeshFilter>().mesh;

  // Inside the room or not? (Rectangular positioned doors only!)
  return roomMesh.bounds.Contains(playerInRoom);

}

So I mostly agree with Timelog - but just add only one trigger and some kind of contains check.

As an extra note, doorway triggers can be really unreliable in some types of game, in which case you’ll want to keep roomMesh.bounds in some room class:

public class Room{

  /// <summary>Transform.position of the room.</summary>
  public Vector3 Position;
  /// <summary>Mesh.bounds.</summary>
  public Bounds RoomBounds;

  /// <summary>Is the given world position inside this room or not?</summary>
  public bool Contains(Vector3 playerPosition){
  
    return RoomBounds.Contains(Position - playerPosition);
  
  }

}

Then you can have an array of rooms and just spam check Contains every ~0.1s (every Update is usually overkill!). If you want bonus performance points, only check it whenever the player is actually moving around - if they’re stationary then they can’t have entered some other room. This way you’ll know for certain which room the player is in without relying on triggers being triggered.

If your rooms aren’t rectangles then you could use a 2D shape library (there’s a bunch of free ones - if you also happen to use PowerUI, it has one in there) and define the outline of the room as a 2D shape, then use the libraries contains functionality and spam that instead of RoomBounds.

These are great responses and im actually studing and working on it.
Ill tell you if i get it working.

Meanwhile, a big THANKS for your time; you are great!

Could it not work exactly as in your picture. Make the collider overlap, so that there are no gaps. Then give all of them a tag “room 1”. As long as the player collides with a collider of tag “room 1” it’s in their. use “on trigger stay” - I think you loose the enter and exit frame but who cares? (You could include them with some little extra work)

I got it. Thanks to all of you, you are SO great!!!

Can you please describe how you solved this for the rest of us.
Thank you