Enter door?
Hello, I'm very new to Unity, and I would like some help with coding. I'm currently making a small 2D side scroller, however I would like the player to be able to enter/exit some buildings. I've attached a photo of what I want exactly to happen. Whenever the player walks in front of a "door" then I want an image to appear (which says press the up arrow to enter), and then upon pressing the button the person enters. How would I go about doing this? Please keep in mind that I'm new and have next to no experience with coding. Using C#.
Answer by PersianKiller · Jan 14, 2018 at 09:38 AM
hi,watch this nice and simple tutorial it will help you.it's very easy :).
part1
part2
if you needed more help ,leave a comment I will help if I could.
Hey, so I did it and it works well. However, whenever I transition to "inside the house" the box colliders of the outside world are still present, so if there was a wall in the outside world, whenever the player transitioned inside the house, the 2D box collider would still be in the way. Any help for this? Thanks!
heres the code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class door : $$anonymous$$onoBehaviour { public GameObject $$anonymous$$art; public GameObject pressButtonImage; public GameObject insideTheHome; public int InsideTheHome;
//
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag ("Player")) {
pressButtonImage.SetActive (true);
}
}
void OnTriggerStay2D(Collider2D other){
if (other.CompareTag ("Player") && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)) {
Debug.Log ("$$anonymous$$eyPressed");
if (InsideTheHome == 0) {
insideTheHome.SetActive (true);
$$anonymous$$art.GetComponent<SpriteRenderer> ().enabled = false;
InsideTheHome = 1;
} else if (InsideTheHome == 1) {
insideTheHome.SetActive (false);
$$anonymous$$art.GetComponent<SpriteRenderer> ().enabled = true;
InsideTheHome = 0;
}
}
}
void OnTriggerExit2D(Collider2D other) {
if (other.CompareTag ("Player")) {
pressButtonImage.SetActive (false);
}
}
}
because it was just an example, simply you can create some GameObjcets for example next House.when you pressUparrow$$anonymous$$ey nextHouse should be disable,so you can't collide with them watch it :)
edit : my bad I forgot to make playerCanGoInsideTheHouse false when the player is not standing near the door change it to this.
void OnTriggerExit2D(Collider2D other){
if (other.CompareTag ("player")) {
pressButtonImage.SetActive (false);
playerCanGoInsideTheHouse = false;//so you cannot press the button
}
}
What? I'm confused at this response, sorry.