How do you make a function available to a character, depending on what they are holding? C#
I am making a short game where the player character is able to pick up a powerup which then allows projectile attacks to happen. I'm quite a noob with coding, and I'm trying this in C#
By default the PC can do melee attacks, but I am trying to figure out how to specify 'if character is holding a gun, they can now shoot bullets'. I have the actual animation and controls prepared, but I know I'm missing something to check if the PC is holding the gun. Here is my code so far (I'm removing the code for other controls from this part):
void Update () {
if (Input.GetButtonDown ("Fire1")) {
animator.SetTrigger ("Punch");
}
if (Input.GetButtonDown ("Fire2"))
{
animator.SetTrigger ("Shooting1Hand"); // plays shoot animation when spell button is pressed
}
}
Any help would be much appreciated!
Answer by b1gry4n · Apr 07, 2016 at 10:45 PM
bool hasGun;
if (Input.GetButtonUp("Fire1")) // order of operations. GetButtonUp will trigger last. ( GetButtonDown > GetButton >GetButtonUp)
{
if (canShoot)
{
//do shooting stuff here
}
else {
//do punching stuff here
}
}
if (Input.GetButton("Fire2") && hasGun) // as the player holds down the button and has a gun
{
canShoot = true; // the player can shoot
}
else {
canShoot = false; // the player can not shoot, they are either not holding the button down OR they dont have a gun
}
GetButtonDown is only true for 1 frame just as the button is pressed. GetButton is true for the duration of the pressed button. GetButtonUp is true on the last frame the button is lifted.