- Home /
Question by
ab4554 · Nov 03, 2014 at 06:14 PM ·
c#animationsprite-animationstopping-animation
How to call an attack animation that will stop when you stop clicking attack button?
We are creating a 2D side-scrolling game using c#. We have a script to call our attack animation when you click 'A'. However we can't figure out how to call the idle/run animation once were done attacking ('A' is released). Right now, once you click 'A' once during game play the attack animation won't stop. I think we need to call the idle/run animation in the empty else statement but not sure how. Here is our script:
public class PlayerAttack : MonoBehaviour { public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.A))
{
Attack();
}
else
{
}
}
private void Attack()
{
GetComponent<Animator>().SetBool("Attacking", true);
float distance = Vector3.Distance (target.transform.position, transform.position);
if(distance < 2.5)
{
Enemyhealth eh = (Enemyhealth)target.GetComponent("Enemyhealth");
eh.ChangeHealth(-10);
}
}
}
Comment
Answer by b1gry4n · Nov 03, 2014 at 07:29 PM
void Update(){
//** OPTION A **
if(Input.GetKeyDown(KeyCode.A)){
attacking = true;
//play attack
}
if(Input.GetKeyUp(KeyCode.A) && attacking){
attacking = false;
//play idle
}
//** OPTION B **
//As long as you hold the button down, attacking will be true.
//Keep in mind this will trigger the attacking = true every update cycle
if(Input.GetKey(KeyCode.A)){
attacking = true;
//play attack
}else{
if(attacking){
attacking = false;
//play idle
}
}
}
Your answer
Follow this Question
Related Questions
2D Character Animation Help 1 Answer
Multiple Cars not working 1 Answer
Swap sprite for a set amount of seconds 2 Answers
Distribute terrain in zones 3 Answers