- Home /
Swap sprite for a set amount of seconds
Hi!
I need some coding advice on a attack animation
What i want to do is when lmb is clicked (mouse0) the PitchforkIdle sprite changes to the PitchforkAttack sprite for 2s. (The PitchForkAttack sprite is suppose to be an animation but for now i use single sprites). When the 2s has passed, the PitchForkAttack sprite changes back to the PitchForkIdle sprite. After the "animation" for 2s is done you cant do the attack again for 1s (so you wont be able to spam the attack). You will also not be able to to the attack again during the "animation".
This is what i have so far:
using UnityEngine;
using System.Collections;
public class SpriteHandler : MonoBehaviour {
public Sprite PitchforkIdle;
public Sprite PitchforkAttack;
private SpriteRenderer spriteRenderer;
void Start () {
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0)) // If lmb is pushed down
{
spriteRenderer.sprite = PitchforkAttack;
}
else
{
spriteRenderer.sprite = PitchforkIdle; // otherwise change it back to idle
}
}
}
When i click mouse0 using this script i only get the Attack sprite for a split of a second.
I guess what i need is couroutine and yield.waitforseconds? I have looked in to these but dont really now how to implement them on my problem.
Anyhow i hope someone can give me some advice on this.
Cheers!
/Taegos
Answer by Alec-Slayden · Jul 19, 2015 at 04:47 PM
Your sprite is only appearing in one frame because you are using GetKeyDown(), which is only true for the first frame a key was depressed. In circumstances where you want to continuously check a state you should use GetKey() instead.
However, I think that you should keep using GetKeyDown, because it sounds like you want your attack sprite to always last two seconds, therefore it doesn't matter if they keep holding the key. Instead of changing that, lets change something else. For one thing, we're not going to want the else condition in there like that, because you're changing the sprite every frame unnecessarily.
A coroutine would work, but since you're already using update and it's a simple script, lets just add to that.
First, we want to know if we have started attacking, so lets add a bool field called isAttacking. You could alternatively just check to see which sprite is being used, but I find a value specifically designed for a state check to be more friendly to alterations to the other fields later.
Next we want to know how long we've been attacking, so lets add a float called attackTimer. We'll add Time.deltaTime into it so it will give us the seconds that have passed.
Now what we'll do is have a key down event set the attacking flag to true. Then we're going to check to see if we're attacking, and if so, the timer will count up until it reaches 2 seconds. Once it's two seconds we'll reset the sprite and stop attacking.
using UnityEngine;
using System.Collections;
public class SpriteHandler : MonoBehaviour {
public Sprite PitchforkIdle;
public Sprite PitchforkAttack;
private SpriteRenderer spriteRenderer;
private bool isAttacking;
private float attackTimer;
void Start () {
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update () {
// set attack sprite and flag the frame that a key is down.
if (Input.GetKeyDown (KeyCode.Mouse0))
{
this.isAttacking = true;
this.spriteRenderer.sprite = PitchforkAttack;
//this.attackTimer = 0f; //option to reset time
}
// while attacking, count up the timer.
if (this.isAttacking)
{
this.attackTimer += Time.deltaTime;
// once the timer is 2 seconds, stop attacking and reset the sprite.
if (this.attackTimer >= 2f)
{
this.isAttacking = false;
this.attackTimer = 0f;
spriteRenderer.sprite = PitchforkIdle;
}
}
}
}
If you want to reset the timer so it is 2 seconds again even if the user clicks in the middle of an attack, just set the timer to zero in the first condition, where the key is down.
The code above is sparse for readability here, but I suggest formatting it tighter for maintainability when used (remove extra line breaks etc)
Wow, thank you so much! This is one of the best answers i have gotten since i signed up here. Very informative and noob-friendly ;) Thanks!
Answer by etopsirhc · Jul 19, 2015 at 04:35 PM
coroutines. spicificaly make a coroutine that has both the animation and the wait time in it, then use an getbutton check to see if it needs to start the animation.
coroutine function
while (item equipped) // or something to make sure it only can trigger when you want it to
if (button down) // trigger animation
start animation
yeild for 2 seconds
stop animation
yeild for 2 seconds