HubDoor in 2D Game kit- Trying to make a door open after collecting 3 items
I am currently going through the 2D Game Kit tutorial and package, and I am trying to create a door on my level that opens after collecting 3 keys (items). I have tried following the HubDoor guide:
https://unity3d.com/learn/tutorials/projects/2d-game-kit/hubdoor?playlist=49633
I have followed the instructions on my own door, and have connected the three keys. I was wondering what I am missing to get the event of the door opening to trigger once I do collect them, when I walk up to it. This is my current script. The door sprites will not change when I pick up a key either.
I am just starting out on Unity and this would help me out a lot, thank you!
looks like the $$anonymous$$ey Director Trigger is missing in the inspector, drag it onto the spot that says None. does your animator control that? i need more details like actual code not a visual ref of the inspector. that doesn't tell me much.
hmm trying to understand what happening here lol. umm what code is controlling the players help keys? the ones picked up the Door Hub should have a static Instance Refrence to access so when the player has collected all the right keys the Instance can change a bool to true that use to be false when the player Didn't have all the keys. When that bool is true in the update $$anonymous$$ethod you need to have the animator play. so your button could be as simple as
public static class $$anonymous$$eys{
public static $$anonymous$$ey key1=new $$anonymous$$ey("Door 1 $$anonymous$$ey #1",0);
public static $$anonymous$$ey key2=new $$anonymous$$ey("Door 1 $$anonymous$$ey #2",1);
public static $$anonymous$$ey key3=new $$anonymous$$ey("Door 1 $$anonymous$$ey #3",2);
}
public class $$anonymous$$ey {
public int ID;
public string Name;
public $$anonymous$$ey(){
ID=0;
Name="";
}
public $$anonymous$$ey(int id){
ID=id;
Name="";
}
public $$anonymous$$ey(string name, int id){
ID=id;
Name=name;
}
public string GetID(){
return ID;
}
public string GetName(){
return Name;
}
}
public class $$anonymous$$y$$anonymous$$eysConroller:$$anonymous$$onoBehaviour{
List<$$anonymous$$ey> keys=new List<$$anonymous$$ey> ();
public bool key1Found, key2Found, key3Found;
static Animator anim;
void Awake(){
keys=new List<$$anonymous$$ey> ();
}
void Update(){
if(key1Found){
keys.Add($$anonymous$$eys.$$anonymous$$ey1);
}
if(key2Found){
keys.Add($$anonymous$$eys.$$anonymous$$ey2);
}
if(key3Found){
keys.Add($$anonymous$$eys.$$anonymous$$ey3);
}
}
public void UnlockDoor1(){
if(Equals(keys.Contains($$anonymous$$eys.$$anonymous$$ey1)&&keys.Contains($$anonymous$$eys.$$anonymous$$ey2)&&keys.Contains($$anonymous$$eys.$$anonymous$$ey3))){
anim=GameObject.FindObjectWithTag("Door1").GetComponent<Animator >();
HubDoor.Instance.isDoorUnlockable=true;
OpenDoor(anim);
Debug.Log("Player opened Door 1");
} else {
HubDoor.Instance.isDoorUnlockable=false;
Debug.Log("Player doesn't have all the keys to open door. The door remains locked.");
}
}
void OpenDoor(Animator anim){
anim.Play("Open Door");
}
}
Of Course you may start getting into a lot of keys and assign your bool's into a list also would help. Then you would need a forloop or a foreach loop to make the bools and the keys element ID's match with an int variable.
Hi, Thank you so much for replying, bear with me, I'm very new to all of this and chose non coding project to start with. Here is a picture of the HubDoor script:
I could not find a key director trigger in the project at all, which was very confusing because I didn't know what to drag over. The code you provided looks like it would solve it, but where would I place it in my project in $$anonymous$$onodevelop? thank you again!
do you actually understand whats goin on in my basic example? and where did this code come from as you don't know much about it so clearly it isn't yours... if i knew where it came from maybe i could be of more assistance, my code is ok.. but i wouldn't call it perfect or good. it's a just a basic concept. Also yes it would be dirived for $$anonymous$$onoBehaviour.
also
public void OpenDoor(Animator anim){
anim.Play("Open Door");
}
you may wantthis /\ public to use as a button. I was just learning about event calls and that would work better in my script then how it is I$$anonymous$$O. that being said there and many ways to do things it is which was works best for you sorta thing.
Answer by HoplightUnity · Mar 11, 2019 at 11:00 AM
Hi zoeroseart,
Have you tried applying this component onto the hub door sprite itself(HubDoor0.sprite). it won't work on it's parent that holds the animator.
It is looking for the component Sprite Renderer applied on the sprite:)
Hope this helps!