if(Input.GetKeyUp(“s”) && Input.GetKeyDown(“p”))
{
Instantiate(shot, shooter.position, shooter.rotation);
}
else if(Input.GetKeyDown("s") && Input.GetKeyDown("p"))
{
Instantiate(shot, downShooter.position, downShooter.rotation);
}
if(Input.GetKeyUp(“s”) && Input.GetKeyDown(“p”))
{
Instantiate(shot, shooter.position, shooter.rotation);
}
else if(Input.GetKeyDown("s") && Input.GetKeyDown("p"))
{
Instantiate(shot, downShooter.position, downShooter.rotation);
}
I don’t think that you can have multiple key-press (input) statements in one if statement. So I’d change it so you only have to press one key.
Are you really wanting &&? Do you want either key pressed or do they both need to be pressed?
If you’re wanting to have either, just change the ‘&&’ to ‘||’…
if(Input.GetKeyUp("s") || Input.GetKeyDown("p"))
{
Instantiate(shot, shooter.position, shooter.rotation);
}
else if(Input.GetKeyDown("s") || Input.GetKeyDown("p"))
{
Instantiate(shot, downShooter.position, downShooter.rotation);
}
What are you trying to achieve?
Update based on feedback:
Ok if I understand what you need this should work:
var isSDown = false;
function Update () {
if(Input.GetKeyDown("s")) {
isSDown = true;
} else if(Input.GetKeyUp("s")) {
isSDown = false;
}
if((isSDown == false) && Input.GetKeyDown("p"))
{
Debug.Log("p Pressed when s is NOT down");
}
else if(isSDown && Input.GetKeyDown("p"))
{
Debug.Log("p Pressed when s is IS down");
}
}
Depending on what function he/she is using depends on if your script works - Using two inputs in one line of code will error in function update(), so the way I would say of getting around this is using coroutines, which are the tricky side of coding, or you can use string variables so that both inputs are stored on one string - therefore bypassing JavaScript Debugging of that line of code.
– DricoJD@DricoJD - what do you mean by "Using two inputs in one line of code will error in function update()" - sorry but that just doesn't make sense. It's perfectly valid and sane to test for multiple conditions in one line of code.
– larku@DricoJD - Naa, this code is perfectly valid in Unity and works as expected (I just tested it): function Update () { if(Input.GetKeyUp("s") || Input.GetKeyDown("p")) { Debug.Log("S up or P down"); } else if(Input.GetKeyDown("s") || Input.GetKeyDown("p")) { Debug.Log("S down or P Down"); } } So, yes it's different for me. Can you show a code sample that demonstrates what you're saying? I agree about using KeyCode.* rather than magic string literals, but this was not relevant to the OP's question.
– larkuI wanted to have to press "s" to crouch. Notice how the instantiate position is different depending on the button. Im trying to make my player shoot form a lower emptyGameObject when hes holding "s" or "crouching" and from a higher emptyGameObject when "s" is not being pressed.
– superluigiSample code:-
function Update () {
if(Input.GetKeyUp(KeyCode.S) || Input.GetKeyDown(KeyCode.P))
{
Debug.Log("S up or P down");
}
else if(Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.P))
{
Debug.Log("S down or P Down");
}
}
That is a fixation on your code, however I do have another code sample that I can re-write from scratch to show you how I would do it.
That code works fine (once the typos are fixed).. I don't see the issue.. But this is not a solution for the OP, my solution should work just fine though.
– larkuKeyCode.s does not exist, only KeyCode.S same for p/P. Last condition has KeyC0de.p instead of KeyCode.p But other than that, Unity is perfectly happy with it.
– larkuThis is different from what I wanted I need my character to instantiate the attack from an emptyGameObject at eye level when pressing p and from a lower emptyGameObject when holding s and p but I think I may have figured it out. Maybe by having the s keydown statement and within it having the additional pkeydown statement I can hold s and press p to fire repeatedly. The way I originally had it it almost sounds like I had to press them down at teh same time.
– superluigi@larku code in the answer is now done, if it is correct and viable please don't hesitate to give me a tasty thumbs up :) @superluigi, your idea can be interpreted in a different way to others, so can you draw your idea out on paper and scan it in?
– DricoJD
ok. Funny thing is if I let go of s and pressed p almost at the same time I could shoot once lol.
– superluigiThis problem with this solution is that both key press events would have to occur in the exact same Update call (one render frame?). This is unlikely to occur (if Unity even delivers more than one keypress event per Update call? Even if it can, you'd have to be very good to get both key presses that close together..) So the issue has nothing to do with 'multiple key press events in one if statement' but is more timing or the mechanism of key-press event delivery in Unity. My solution below shows a simple way to handle this - there may be other better solutions but it is the most obvious.
– larku