- Home /
Different attacks using same keybindings?
I'm designing a rather simple combat system driven by animations.
I'm trying to create a couple of attacks with the following keybinds:
Mouse 1 = Simple Attack Mouse 2 = Different Simple Attack LeftCtrl + Mouse1 = Power Attack.
However, Unity seems to register only the mouseclick when I'm holding LeftCtrl. I'm almost certain this is just down to me being a noob at coding.
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour {
Animator anim;
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (anim.GetBool("CombatStance") == true)
{
if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("LeftPowerAttack");
}
else if (Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("LeftAttack");
}
else if (Input.GetButtonDown("Fire2"))
{
anim.SetTrigger("RightAttack");
}
}
}
}
Answer by Dibbie · Nov 21, 2016 at 08:15 AM
GetKeyDown, registers ONCE when the user actually presses the key stroke, doesnt have to lift their finger off the key, just as soon as the computer registers "Left Control was pressed", it considers it down, it doesnt check "Left Control is still down" -- thats what GetKey does, itll continue to send "Left Control is held down" rather then "Left Control is pressed".
So you could try structuring your if-statements to look like:
if (Input.GetKey(KeyCode.LeftControl) && Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("LeftPowerAttack");
}
else if (Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("LeftAttack");
}
else if (Input.GetButtonDown("Fire2"))
{
anim.SetTrigger("RightAttack");
}
Hope that helps.
Answer by Steveo22 · Nov 21, 2016 at 08:18 AM
Hello Decreationist,
Based on that code, the LeftPowerAttack should be happening. I haven't messed around with checking two inputs within a single if statement, but as I said, that should be working. Two options for improvement immediately come to mind: 1. You could nest the check for leftcontrol, so like
if (Input.GetButtonDown("Fire1"))
{
if(Input.GetKeyDown(KeyCode.LeftControl)){
anim.SetTrigger("LeftPowerAttack");
}
else{
anim.SetTrigger("LeftAttack");
}
should work.
You could have Left control be stored in a different boolean variable and have it check that instead of the actual GetKeyDown.
I also just noticed that you're using GetButtonDown for fire1, which would work if you have it set up as a button. As far as I am aware, though, "fire1" is defaultly an Axis, which means that you would measure it as a float with a range from -1 to 1. You would check this by typing if(Input.GetAxis("fire1" > 0f))
You can go into Edit > Project Settings > Input to change whether or not it is an Axis
And I also just realized that you're using Get$$anonymous$$eyDown for leftControl. This should be Get$$anonymous$$ey, since Get$$anonymous$$eyDown only checks if the key has been pressed THIS FRA$$anonymous$$$$anonymous$$ Let me know how it goes.
Get$$anonymous$$eyDown was the issue, and changing it to Get$$anonymous$$ey solved the issue. Thanks.
Your answer
Follow this Question
Related Questions
BEGINNER: why is my main menu not loading level 1 when I press play? 1 Answer
main menu not loading level 1 please help beginner here 0 Answers
Newbie question 2 Answers
Script problem : beat'em up 0 Answers
Timed Attack System 1 Answer