- Home /
User can jam on the attack button and get the player stuck in the attack animation.
0 down vote favorite
I am having a problem where the user can jam on the attack button and get the player character stuck on the last frame of the animation. In short my method for melee attack works like this.
Get input from the Player.CS then the script (animationController.CS) on the Animator attached to the Player sprite sees this and tells the Animator to use the attack animation. Then on the attack animation is an Animation Event that calls a function off of a key frame in the animation that spawns the collision object for the player's attack. When the collision object is destroyed the player is supposed to be allowed to attack again. But if the user presses attack too fast they player gets stuck at the last frame of the animation.
So my question is am I doing this wrong? Is this a bad method to deal with animations spawning the collision of a melee attack? Or have I screwed up the logic somewhere that I am just not seeing?
//This is in the Player.CS //If Player presses the attack button
if (Input.GetKeyDown(KeyCode.X) || Input.GetButtonDown("Attack") )
{
//If we have not spawned the primary weapon and x has not already been pressed
if (!hasSpawnedPrimaryWeapon && !xPressedToAttack)
{
xPressedToAttack = true;
}
}
// The above sends a message to the Player Sprite Animator.CS This does not do anything other than trigger the attack animation to play.
if (playerScript.xPressedToAttack == true && playerScript.isDucking == false)
{
playerAttackingPrimary = true;
} else playerAttackingPrimary = false;
//Later in the same script
if (playerAttackingPrimary == true)
{
changeState(STATE_ATTACKINGPRIMARY);
}
// Then using the animation event system when the attack animation is at the point of attack a function is called to spawn the primary weapon from the //Player Sprite Animator.CS
public void isFinishedAttacking()
{
playerScript.spawnPrimaryWeapon();
}
// Later the animation event system when the attack animation is finished a function is called to clear the xPressedToAttack flag // from the Player Sprite Animator.CS
public void clearXandSpawnFlag() {
playerScript.xPressedToAttack = false;
Debug.Log("PrintEvent: setting xPressedToAttack to False at " + Time.time);
}
// The spawnPrimaryWeapon() is located in the Player.CS.
public void spawnPrimaryWeapon(){
GameObject primaryWeapon = Instantiate(primaryAttack, PlayerHead.transform.position, Quaternion.identity) as GameObject;
primaryWeapon.transform.parent = controller.transform;
hasSpawnedPrimaryWeapon = true;
}
// The PrimaryWeapon.CS works like this. When the lifetime is up it destroys it self. It sets the hasSpawnedPrimaryWeapon to false. The lifetime is a public int that is set to 7 when spawned.
void Update () {
lifeTime = lifeTime - 1;
if (lifeTime <= 0){
playerScript.timeOfAttack = Time.time;
playerScript.hasSpawnedPrimaryWeapon = false;
Destroy(gameObject);
}
}
// This is supposed to then allow the user to attack again but they seem to be able to attack again before this is complete. // If the user jams on the button the xPressedToAttack can become stuck on true and is then never set to false.
Your answer
Follow this Question
Related Questions
How can I animate a GameObject that isn't parented, but will need to be for the animation? 1 Answer
How to trigger an event if the collided object has the tag “Player” 3 Answers
chronological order of collision to give a winner unity 2d 4.3 1 Answer
How do I create two colliding Spheres? 1 Answer
Stop Root Motion if out of boundaries? Simple Wall Climbing System 1 Answer