- Home /
Scripting a Konami Code
I want to put the konami code as an easter egg in a game that I'm making but I have no idea how. I know that in the HTML version of java script there is a function but the java script that is used in unity is slightly different. Is the function in the unity version of java script? If not, how would I go about scripting that? Thanks for your help.
While I'm having a hard time finding a solution for JS, Stack Overflow has an example in C#. It looks like you have to dig into the keyboard handling events of .Net :(
Answer by save · Jun 20, 2011 at 07:48 PM
Here's a solution (now solved):
private var konamiCode = ["UpArrow", "UpArrow", "DownArrow", "DownArrow", "LeftArrow", "RightArrow", "LeftArrow", "RightArrow", "B", "A", "Return"];
private var currentPos : int = 0;
private var inKonami : boolean = false;
function OnGUI () {
var e : Event = Event.current;
if (e.isKey && Input.anyKeyDown && !inKonami && e.keyCode.ToString()!="None") {
konamiFunction (e.keyCode);
}
}
function Update () {
if (inKonami) {
//Easteregg!
}
}
function konamiFunction (incomingKey) {
var incomingKeyString = incomingKey.ToString();
if(incomingKeyString==konamiCode[currentPos]) {
print("Unlocked part "+(currentPos+1)+"/"+konamiCode.length+" with "+incomingKeyString);
currentPos++;
if((currentPos+1)>konamiCode.length){
print("You master Konami.");
inKonami=true;
currentPos=0;
}
} else {
print("You fail Konami at position "+(currentPos+1)+", find the ninja in you.");
currentPos=0;
}
}
Funny function to implement! :-)
I was trying to figure this out, too... For the line:
if (e.is$$anonymous$$ey && Input.any$$anonymous$$eyDown && !in$$anonymous$$onami && e.keyCode.ToString()!="None") {
I'm having trouble understanding why pressing the letter keys kept giving a "None". Any thoughts?
Nice implementation, by the way.
Thanks Chris! It seems like it fires a double event due to Control, checking with:
var e : Event = Event.current;
if(e.is$$anonymous$$ey && Input.any$$anonymous$$eyDown){
print("$$anonymous$$EYPRESS "+e.keyCode);
}
seems to trigger the action just once while Control or Command is pressed.
Your answer
Follow this Question
Related Questions
multiple inputs 2 Answers
Multiple Animations, Please Help 1 Answer
how to change default third player controls in unity, to work with touch? 1 Answer
Road Texture Is Stretching 3 Answers
Play sound when out of ammo 0 Answers