Hello,
Been practicing Unity by working on a platformer, where the playable character has a melee attack, which triggers an animation where the melee weapon comes out. My game was originally made using Unity’s old input system, but I recently decided to adjust it so that it uses the newer one instead.
I finally got done setting up the player character’s controls, only to find that the melee attack wasn’t working. After a lot of investigation, I discovered that for some reason, it’s because the animation isn’t activating, for whatever reason. It had worked perfectly fine without a single problem when using the old input system.
The player script is quite long, so I’ve narrowed down only the parts that involve the melee animation:
void Update()
{
if (anim.GetBool("Melee")) //If the melee attack animation has finished
{
ResetMelee(); //Activates the Reset Melee function
}
}
public void MeleeAttack() //Function for melee attack
{
if (!manaSystem.isLow) //If the player has at least two mana
{
melee = true; //Switch on the melee condition
anim.SetBool("Melee", true); //Activates the melee attack animation
if (!burst) //If the player isn't in burst mode
manaSystem.MinusTwo(); //Reduce the player's mana by 2
}
}
public void ResetMelee() //Function for ending the melee attack
{
melee = false; //Switch off the melee condition
anim.SetBool("Melee", false); //End the player's melee attack
}
public void Melee(InputAction.CallbackContext context) //Performing a Melee Attack with the new input system
{
if (started) //If the level has started
{
if (fromPause) //If returning to the game from the Pause Menu
{
if (context.performed) //If any action button is pressed
freezeControls = true; //Prevent the player character from registering inputs rapidly
else //If no button is being pressed
{
fromPause = false; //Recognise that the pause menu is no longer active
freezeControls = false; //Enable player inputs
}
}
if (!freezeControls && !selectMenu && context.performed && !healthSystem.isDead && !stun && !grabbed && !casting && !bonusStage && !shieldActive) //If the player presses the Melee button
MeleeAttack(); //Activates the Melee Attack function
}
}
If the entire script is needed to further figure this out, please let me know and I’ll happily provide it.
I’m currently 100% sure that the only problem is that the animation won’t trigger, as I’ve added Debug.Logs to all parts of the melee attack’s process as shown above, and the Debug.Log appeared at every stage, but without the attack animation (even the player’s mana is reduced when I press the attack button). I also tried commenting out the “ResetMelee” function, but that didn’t make a difference. I even changed anim.SetBool to a different animation, and the other animation worked fine. But when I change it back to melee, I get nothing again. I’ve truly exhausted everything I can think of as to why the animation no longer plays since I changed the input system.
One other factor I think might be the cause (though another part of me is skeptical), is that I sometimes get a red error message saying:
[worker0] tried select unknown importer for id ‘-2’ ‘00000000000000000000000000000000’
However, even when I don’t get this message, the animation still doesn’t work.
These are the current settings for the animation, and the object’s animator:
One more thing to note: I have a pause menu which, when active, sets the game’s speed to 0f (I know this probably isn’t the best way to handle pause menus as it’s given me difficulties, but I was following a tutorial and managed to make it work as I wanted regardless). Now, I recently fixed a problem where the player’s inputs would be read while the game was paused, so that when I unpause it, the character will do all the inputs immediately when the game speed resumes. The reason I’m mentioning this, is because this has been the only way I could get the melee animation to play since changing to the new input system. I would press the attack button while the game is paused (and see that the player’s mana decreases), then when I unpause the game, the animation would actually play.
I’ve since adjusted it so the player inputs are not read while the game is paused, but even then, the melee animation wouldn’t play during actual gameplay. Every other animation the character has works fine, except for the melee attack, and I honestly can’t see why, especially considering it worked perfectly fine before I changed the input system.
I’m really hoping someone can help me figure this out, because I’ve exhausted any solutions I can think of myself. It’s like the new input system just doesn’t like this one particular animation for whatever reason (even though most of the others have the same settings, but play perfectly).
I also think it’s worth mentioning that the player object is a prefab, if that makes any difference.
Thanks in advance.