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 MikeErty · Jul 18, 2013 at 12:14 PM · if-statementscharacter movementelse

stealthy/not stealthy input detection not working

My code checks to see what combination of keys are pressed and updates two variables as needed. If the player is moving (only forward for now) at normal speed then he is 'moving'(for the purposes of this 'moving' means 'moving at normal speed'. When LeftShift is held down I want it to make 'moving' false and 'stealthy' true.

At the moment it successfully sees moving as true if going forward, but when shift is pressed during this time then both are set to false rather than true for stealthy. stealthy doesn't work on its own either. I got it to carry out commands with an IF instead of an ELSE but i'm after an else here. I tested changing the second if to an else too and it did the same thing - wouldn't acknowledge the input.

It seems simple and straightforward but it's not working...

 var AudioFile : AudioClip;
 var slowAudio : AudioClip;
 var moving : boolean;
 var stealthy : boolean;
 
 
 
 
 function Start()
 {
     moving = false;
     stealthy = false;
 }
 
 
 
 
 
 function Update() {
 
     //check to see if character is moving normally
 
     if (Input.GetKeyDown (KeyCode.W))
     {
         moving = true;
     }
     
     if (Input.GetKeyUp (KeyCode.W))
     {
         moving = false;
     }
     
     
     //check if shift is being held and make character stealthy
     
     if (Input.GetKeyDown (KeyCode.LeftShift))
     {
         moving = false;
         stealthy = true;
         Debug.Log("stealthy");
         
     }
     else
     {
         stealthy = false;
         Debug.Log("not stealthy");
     }
     
     
     
 }
Comment
Add comment · Show 2
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 wizdis · Jul 18, 2013 at 02:01 PM 0
Share

I have another suggestion. $$anonymous$$aybe you should abstract away the exact keys that are being pressed, and represent them as the action which you are trying to develop logic for. Introducing this "abstraction" layer would allow you to more easily provide the user with the option to change their keybindings. Just a thought.

avatar image MikeErty · Jul 18, 2013 at 02:32 PM 0
Share

Cheers guys, I'll certainly remember that for future reference. Oh and sorry brag I only just noticed you asked 'will you ever need to be s$$anonymous$$lthy while standing still' and the answer is no. Thanks again guys.

2 Replies

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

Answer by brag42 · Jul 18, 2013 at 01:02 PM

You are using Input.GetKeyDown and Input.GetKeyUp, which only return true during the frame they are pressed. That's not what you want. Instead, use Input.GetKey so you know that the keys are continually held down.

Are you mutually excluding Walking and Stealthy? Do you want the option to walk stealthily by pressing W and LeftShift? If so, you should next the Input.GetKey(KeyCode.LeftShift) inside the if statement for Input.GetKey(KeyCode.W)

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 MikeErty · Jul 18, 2013 at 01:23 PM 0
Share

Yes, this was exactly the intended mechanic. $$anonymous$$oving forward and then entering 's$$anonymous$$lth mode' whenever you press shift. Similar to how Counter Strike or most FPS' work, letting you walk rather than run. I'll give this a go too :)

avatar image amphoterik · Jul 18, 2013 at 01:32 PM 0
Share

Will you never need to be s$$anonymous$$lth while standing still?

avatar image MikeErty · Jul 18, 2013 at 01:39 PM 0
Share

This worked fine:

 function Update() {
 
     //check to see if character is moving normally
 
     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W))
     {
         //and make character moving
         moving = true;
         Debug.Log("is moving");
         
         
             //check to see if sneak key is being held
             
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift))
             {
             //and make character sneaky
             
                 s$$anonymous$$lthy = true;
             }
             
             else
                 s$$anonymous$$lthy = false;
     }
     
     else
         moving = false;
 }

Thanks a lot, I never considered putting if statements within other if statements.

avatar image
1

Answer by amphoterik · Jul 18, 2013 at 12:23 PM

EDIT: you have already selected another answer, but for completeness sake I have updated my code with the solution you seek.

You are treating the two as mutually exclusive, and that is going to cause issues for you logically later. Why can't you be moving AND stealthy? Seems reasonable to me. So with that in mind, let's change some things up:

 if (Input.GetKeyDown(KeyCode.W))
 {
     moving = true;
     if (Input.GetKey(KeyCode.LeftShift))
         stealthy = true;
     else
         stealthy = false;
 }
 if (Input.GetKeyUp(KeyCode.W))
 {
     moving = false;
     stealthy = false;
 }

Another issue this solves is input fighting. Consider your code above. If I am moving forward and continue to hold the W key when I press the shift key. I am not no longer "moving" and instead am stealthy. Now, assuming I am still holding the W key, when I release the shift key I am no longer stealthy and also no longer moving. Well, why wouldn't I be moving if I am holding the W key? The solution is to make them work together as oppose to making them conflict.

You had mentioned wanting to play sounds based on how the character was moving. This is actually easier if you treat them differently. For example, I would do:

 if(moving)
 {
     if(stealthy)
         //play stealthy sound
     else
         //play non-stealthy sound
 
 }
Comment
Add comment · Show 5 · 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 MikeErty · Jul 18, 2013 at 12:50 PM 0
Share

Thank you for the reply, I appreciate it. I tried what you said and I see what you're getting at. I noticed you had no braces around your else statements (I'm only about 3 or 4 months into program$$anonymous$$g) so I removed the ones I had.

I also changed the script to what you suggested but now it doesn't apply the change to the variable. I added a debug.log of "is moving" into moving = true part and the debug message pops up but the actual var doesn't change to true... I assume it's something to do with the else because changing it to an if statement again let's the var turn off and on.

The reason I Wanted to have moving OR s$$anonymous$$lthy was so I could apply functions that would play a certain sound effect for moving normally and one for moving at a slowed, s$$anonymous$$lthy pace as well as controlling speed. But having moving AND s$$anonymous$$lthy would still be useful, I could simply say that if both were active then it should play the s$$anonymous$$lthy sound.

avatar image amphoterik · Jul 18, 2013 at 12:53 PM 1
Share

Regarding the else statements: If you only have one line of code following and IF or ELSE, then you can remove the braces. Otherwise they need to be there.

Let me edit the IF statements in my code to see if I can't fix your issue. I don't have Unity in front of me so I can't test them myself.

avatar image MikeErty · Jul 18, 2013 at 01:46 PM 0
Share

Thanks again for your efforts :)

avatar image brag42 · Jul 18, 2013 at 01:59 PM 2
Share

@$$anonymous$$ikeErty: amphoterik's advice for IF/ELSE statements is correct. If you are a pretty new programmer then just realize that having braces around a single line of code is ok. And, especially because you are a new programmer, I recommend that you use braces anyway. It will save you major headaches later on if you decide to add more than that single line of code. You don't want to have those problems to debug. I've been program$$anonymous$$g for a long time and I still leave braces in there because then I don't have to worry about looking for braces when I add code. Hope this helps.

avatar image amphoterik · Jul 18, 2013 at 02:19 PM 1
Share

What Brag42 said. Solid advice.

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

18 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

Related Questions

Need Help Improving If Else statement 1 Answer

Prevention of air movement 1 Answer

True and False values with If and else 1 Answer

Gap in requirements in GUI based game 2 Answers

Multiple Else Statments? 2 Answers


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