unity3d - unity2D C# - player must press two switches to open door -
essentially have door trigger event if player presses switch door open. wish if door associated needs 2 switches open well... opens if 2 switches pressed. here code interactswitch
public class interactswitch : monobehaviour { animator animator; public dooreventtrigger[] doortriggers; public bool pressed; private bool down; // use initialization void start () { animator = getcomponent <animator> (); } // update called once per frame void update () { } void oncollisionenter2d (collision2d target) { if (target.gameobject.tag == "player") { down = true; animator.setinteger ("switchtrig", 1); foreach (dooreventtrigger trigger in doortriggers) if (trigger != null) trigger.toggle (true); } }
when triggered event checks player, shows switch has been pressed sends bool function called toggle handles operation of door.
next have dooreventtrigger event checks if bool sent = true. if door open. here stuck. seen in code have created array of interactswitch stores amount of switches want player have pressed before door open. state if length > 1 if condition true , code added here open door if player has selected interactswitch[].length switches. question how check instances of interactswitch dooreventtrigger equals true?
here dooreventtrigger code
public void toggle (bool val) { if (switchtriggers.length > 1) { debug.log ("has done anything"); door.open (); } else { if (val) door.open (); else door.close (); } }
}
the solution came near believe efficient answer requires code each time u add new switch have
firstly in interactswitch script bool down public can accessed switchtriggers
public bool down;
next added code toggle() method
public void toggle (bool val) { if (switchtriggers.length > 1) { if((switchtriggers[0].down == true) && (switchtriggers[1].down == true)){ debug.log ("this done something!"); door.open (); } } else { if (val) door.open (); else door.close (); } }
now public switchinteract[]; know exact amount of elements in array checked through switchtriggers.length; knowing there 2 elements in array condition switchtriggers.length > 1 correct next take 2 elements in array , check if down; if both down condition continues , debug.log() validates this. open() called opens door.
Comments
Post a Comment