So I have a walking/idle sprite and a shooting sprite. When I call the shoot function I want the sprite to change to the shooting one but after it has finished shooting go back to the idle sprite.
This is what I have come up with:
private Sprite[] shootSprite;
// Use this for initialization
void Start()
{
shootSprite = Resources.LoadAll<Sprite>("spritesheet_characters");
}
// Update is called once per frame
void Update()
{
if (fireRate == 0) //if single burst fire
{
if (Input.GetMouseButtonDown(0)) //set Fire1 in input manager
{
Shoot();
}
}
else
{
if (Input.GetMouseButtonDown(0) && Time.time > timeToFire)
{
timeToFire = Time.time + 1 / fireRate;
Shoot();
}
}
}
void Shoot()
{
gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[15];
//rest of code related to shooting.
}
Now the problem is that once I click it goes to shooting sprite and then stays there, it should go back to previous sprite.
I tried putting:
gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[39]; //idle sprite
within Update()
but that does not work well as the transition is very fast and very rarely do I see the sprite change during shooting.
Also I dont want to be changing sprite every update as I am using an animator to rotate it while walking.
Is there any way to do this?
Thanks