x


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.

more ▼

asked Jun 20 '11 at 06:36 PM

Ryan 8 gravatar image

Ryan 8
48 5 5 11

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 :(

Jun 20 '11 at 07:43 PM Chris D
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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! :-)

more ▼

answered Jun 20 '11 at 07:48 PM

save gravatar image

save
8.2k 22 31 62

I was trying to figure this out, too... For the line:

if (e.isKey && Input.anyKeyDown && !inKonami && 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.

Jun 20 '11 at 09:01 PM Chris D

Thanks Chris! It seems like it fires a double event due to Control, checking with:

var e : Event = Event.current;
    if(e.isKey && Input.anyKeyDown){
       print("KEYPRESS "+e.keyCode);
    }

seems to trigger the action just once while Control or Command is pressed.

Jun 20 '11 at 09:13 PM save

That's awesome, thanks.

Jun 20 '11 at 09:43 PM Ryan 8
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3465
x956
x536
x32
x22

asked: Jun 20 '11 at 06:36 PM

Seen: 840 times

Last Updated: Jun 20 '11 at 09:47 PM