- Home /
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");
}
}
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.
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.
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)
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 :)
Will you never need to be s$$anonymous$$lth while standing still?
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.
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
}
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.
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.
@$$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.
Your answer
Follow this Question
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