- Home /
Entering 2D Buildings?
Hello,
I'm trying my hand at a 2D side-scrolling/platformer action rpg in Unity2D (I know, I know), and I was wondering what's the best way to deal with buildings in 2D. I find entering buildings easy to envision in a 3D world, but when it comes to the camera being locked to stay mostly on the x and y axis, I can't quite wrap my mind around it. Here's an example of what I would like from Muramasa: The Demon Blade: https://www.youtube.com/watch?v=SAHME_7mizg#t=1920
I understand that triggers are needed but I'm confused about where the interior of the building would be, so to say. I'd like the characters to run in front of the facade of the building with a trigger over the front door allowing them to press whatever button to enter. The inside of the building would be a side-view too, like in the example I showed above.
Should it be a new scene entirely? (I imagine that could make things way too complicated and slow for a lot of buildings). Or should I just build it separately somewhere in the current scene and have the trigger at the front door of the facade teleport them to inside the building, where a fixed camera would be enabled? I'm planning future indoor levels where there are several rooms (sort of like a hotel corridor), so having possibly so many separate rooms could make things difficult.
I was also toying with the idea of having only a few different variations of the interior of buildings per level decoration-wise, and just randomizing what spawns inside of them.
Thank You for the Help, Eva
Answer by Xevoius · Apr 30, 2014 at 02:51 PM
I think this all depends on how large your rooms are. In my game, I have one master room chamber scene with doors to all the other rooms that are scenes of their own. Every room is a level and can be made up of smaller room areas with doors that teleport the player around this same room (scene).
I maintain a collision list on the player object. This keeps all the objects that the player is currently colliding with so that I can iterate over it when the player presses the action button.
When the player collides and OnTriggerEnter2D is called, the object is added to the list and when the player stops colliding OnTriggerExit2D, the object is removed from the list. This allows me to give the collision some logic say when there is a pickup object that is in front of the door and the player is colliding with both the door and the pickup object.
As far as graphics goes, start using the SpriteRenderer Sorting Layers to make sure objects appear on top of one another how you want them. They are named layers and make things easy to organize on the screen as opposed to adjusting their Z value.
One other important note: OnTriggerEnter2D and OnTriggerStay2D and OnTriggerExit2D seem to be bugged in the current release. I still use them in my project with some workarounds that I am hoping I will be able to remove sometime soon.
http://issuetracker.unity3d.com/issues/ontriggerenter2d-called-too-many-times http://issuetracker.unity3d.com/issues/ontriggerstay2d-is-called-not-properly
Cheers.
Answer by GEWLAR · Apr 30, 2014 at 01:13 PM
For entering a building you could use something like this:
Attention this is only pseudocode
public Vector3 Teleportposition
void OnTriggerStay (collider other)
{
if( collider.tag == "Player" and input.getkey("w"))
{
//Go into house
LastPosition = other.transform.position
other.transform.position = Teleportposition
}
}
I would use there OnTriggerStay because this gets called every frame. So while you stay in the Trigger and press the key the Teleport Code gets called.
I think a Scene for every room is to much. Especially if you have several rooms in a building. So I would create the room by itself and then teleport the player to the new location.
I recommend to save the Players-Lastposition in a Variable. I would save the Variable in a Script on the Player. If you only have one Player in the Game you can make this Variable a static Variable.
Hope this is helpful for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143854im_/https://answers.unity.com/themes/thub/images/avi.jpg)