• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by darkhog · Oct 11, 2013 at 06:45 AM · cheat

How to implement cheat codes

I don't want to go "console route" for inputting cheat codes. I'd like something like e.g. in first Doom where there is no visible or audible feedback until cheat is correctly entered. How would I go about that?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

8 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by neshius108 · Sep 06, 2018 at 06:27 PM

Yet another version.

Based on @mattssonon but I find this a bit easier to setup and use. Enjoy!

It makes use of UnityEvent to being able to pick any public function. And lets you pick any KeyCode instead of just plain strings.

 using UnityEngine;
 using UnityEngine.Events;
 
 public class CheatInput : MonoBehaviour
 {
     public KeyCode[] CheatCode;
     public UnityEvent CheatEvent;
     public float AllowedDelay = 1f;
 
     private float _delayTimer;
     private int _index = 0;
 
     void Update()
     {
         _delayTimer += Time.deltaTime;
         if (_delayTimer > AllowedDelay)
         {
             ResetCheatInput();
         }
 
         if (Input.anyKeyDown)
         {
             if (Input.GetKeyDown(CheatCode[_index]))
             {
                 _index++;
                 _delayTimer = 0f;
             }
             else
             {
                 ResetCheatInput();
             }
         }
 
         if (_index == CheatCode.Length)
         {
             ResetCheatInput();
             CheatEvent.Invoke();
         }
     }
 
     void ResetCheatInput()
     {
         _index = 0;
         _delayTimer = 0f;
     }
 
     public void Cheat()
     {
         Debug.Log("CHEAT ACTIVATED");
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Shaolin-Dave · May 14, 2018 at 10:17 PM

I just threw this together so you might need to tweak it (i.e. I didn't confirm the keycodes). This example would start the game when the player presses "Enter" on the keyboard. Normally they have 3 lives, but if they enter the infamous "Konami Code", they get 30 lives:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KonamiCode : MonoBehaviour {
 
     private String inputString;
 
     void Update ()  {
         if (Input.GetKeyDown(KeyCode.UpArrow)) {
             inputString += 'U';
         } else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             inputString += 'L';
         } else if (Input.GetKeyDown(KeyCode.DownArrow)) {
             inputString += 'D';
         } else if (Input.GetKeyDown(KeyCode.RightArrow)) {
             inputString += 'R';
         } else if (Input.GetKeyDown(KeyCode.A)) {
             inputString += 'A';
         } else if (Input.GetKeyDown(KeyCode.B)) {
             inputString += 'B';
         } else if (Input.GetKeyDown(KeyCode.Enter)) {
             if (inputString.endsWith('UUDDLRLRBA')) {
                 StartGameWithLives(30);
             } else {
                 StartGamesWithLives(3);
             }
         } 
     }
 }

Some perks to doing it this way, it'll allow for starting over because it only checks the end of the string. If they mess up the code three times but then get it right on the last one, it'll still work. Also, you can easily add codes. Just add a new "endsWith" condition inside the block where "Enter" is pressed. There's all sorts of tweaks you can make to this. I've also built off of this to create a "Street Fighter"-style special moves input script.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lagger625 · Sep 12, 2018 at 12:41 AM 0
Share

omg your approach is simple and clever compared to $$anonymous$$e...

The thing here is, if you keep playing and pressing keys the inputString will keep growing bigger and bigger, wich will cause a growing-over-time waste of memory and CPU resources (related to garbage collection and concatenation of strings using "+" operator).

You should use some other way of keeping track of the last pressed keys.

avatar image Shaolin-Dave · Sep 12, 2018 at 05:36 AM 0
Share

In this particular case, this code would only be relevant on the title screen. I don't think players would spend that much time there pushing random buttons. In my "Street Fighter"-style application, I had the method consider in the string ended with any special move key combination, execute the proper method if it did, execute a basic attack method if it didn't, and then clear out the string. It was never longer than whatever it gathered in-between punches/kicks. Depending on the application, you can have different methods of controlling the length. A new strategy I just thought of would be a looping coroutine (maybe every 10 seconds or so) that trims the string down to it's last 10 characters (or however many the longest cheat code is).

avatar image
0

Answer by toddisarockstar · Mar 04, 2017 at 10:27 PM

nothing wrong with matts answer i just wrote this for my game and i thought i would share. it limits the time between keystrokes so it has to be typed adjustably fast. just just reminded me of old school gaming. this code works with an x box controller too:

     float quickness;
     String cheater;
     int ii;
     void Update () {
         ii = 9;
         while(ii>0){ii--;
             if (Input.GetKeyDown ("joystick 1 button " + ii)) {
 
                 cheater=cheater+""+ii;
                 quickness=0.8f;
 
             }
             }
 
         if (Input.inputString != "") {
             cheater = cheater + Input.inputString;
             quickness = 0.8f;// <---time delay between buttons
                 }
 
         if (quickness >= 0) {
                         quickness += -Time.deltaTime;
                         if (quickness < 0) {
         print ("user is attempting cheat code: "+cheater);
         if (cheater == "boom") {print ("blow stuff up something here");}
         if (cheater == "allweapons") {print ("blow up everyone here");}
                 if (cheater == "funkychicken") {print ("i think you get the point");}
         if (cheater == "001133") {print ("i pushed A A B B Y Y on my controller");}
 
                     cheater="";}}}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Glitches and Cheat Codes 3 Answers

Cheat Code Box 1 Answer

How to fly with cheat code ? 0 Answers

Trying to Make a Topdown, 3rd Person Ladder 0 Answers

Access variable in another script beginner 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges