Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by PanStefanb01 · Oct 09, 2014 at 04:25 PM · orderspecificmagicpressing

Pressing Keys in a Specific Order

Hi! I am making a game involving magic and I have a great idea how to cast spells. But I don't know what to write. This is what I want the console to say (not exactly).

//User pressed and is holding E, Starting Spell recognision

//Pressed LeftClick

//Pressed LeftClick

//Pressed RightClick

//Pressed LeftClick

//User let go of E, Recognising spell

//Spell is "Fire", Casting spell

BTW: Code can be in any language as long as you explain what each part does and what I need to change to add more spells.

Comment
Add comment · Show 1
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 Spinnernicholas · Mar 30, 2015 at 09:29 PM 0
Share

Please accept my answer if it helped, otherwise give me more information. This hurts my answer acceptance rate.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Spinnernicholas · Oct 09, 2014 at 04:35 PM

Use Input.GetKeyDown and Input.GetKeyUp to determine when the user starts and stops holding the 'E' key down.

Then use Input.GetMouseButtonDown while E is being pressed to read mouse clicks. Note that:

button values are 0 for left button, 1 for right button, 2 for the middle button.

While tracking the clicks, add 'L' and 'R' to a "Spell String".

Then you just have to check it against your library of spell strings.

 var fireBallSpell = "LLRLLRRL";
 
 if(inputSpell == fireBallSpell)
 {
   castFireBall();
 }


Comment
Add comment · Show 3 · 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 Anxo · Oct 09, 2014 at 04:43 PM 1
Share

Uhhh, I like this better!

avatar image Spinnernicholas · Oct 09, 2014 at 04:59 PM 0
Share

Thank you, since you don't have to check anything until after a key is released, you can simplify it quite a bit. And you can actually create a dictionary to register spell strings to objects.

"LRLRLRLLLLR" => "FireBallScriptComponent"

$$anonymous$$ake all spell scripts derive from a single parent class with an abstract method Cast();

Then you can do:

 if(this.spellBook.Contains$$anonymous$$ey(spellString))
 {
   ((SpellScript)gameObject.GetComponent(this.spellBook[spellString])).Cast();
 }
avatar image Anxo · Oct 10, 2014 at 04:27 PM 0
Share

Very cool. The Lambda expression is a little too deep for most beginners but great method.

avatar image
0

Answer by Anxo · Oct 09, 2014 at 04:41 PM

This can just be solved with bools or stages. User holding down E can be handled like so

 private bool eIsHeldDown = false;
 public int FireSpellStage = 0;
 
 void Update(){
   eIsHeldDown = Input.GetKey(KeyCode.E);
   if(eIsHeldDown){
        if(FireSpellStage == 0 ){
              if(Input.GetKeyDown(KeyCode.F)){
              FireSpellStage = 1;
              }
              else if (FireSpellStage == 0){
                     if(Input.GetKeyDown(KeyCode.I)){
                            FireSpellStage = 0;
                      }
             } 
            // And So on, till you made a combo you like. 
         }
 
   }
 else{
      if(FireSpellStage == 4){
           Debug.Log("Casting Fire Spell");
      }
 FireSpellStage = 0;
 }
   
 }


WUC (warrning, uncompiled code, check for errors. )

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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Keys pressed in specific order 1 Answer

Can you tell me more about Unity's Human Interface Devices? 1 Answer

is the unity player browser specific? 1 Answer

How to use spriteRenderer.sprite for a GameObject in a list? 0 Answers

How to string multiple audio clips together 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