Syncing Shot with Animation

So I’m currently making a 2D platformer game with unity, and our character is able to shoot.
And we have 3 animations, 1 for preparing a shot, 1 for the actual shot, and then the preparation animation played backwards.

And I even managed to create mecanim states that work pretty good, the thing is now, my bullet spawns immediately. So how can i make my shoot script wait, until it is in the “Attack” State, at a certain keyframe, so i can assure, the shot is correctly synced up with the animation?

The problem is, i could use events and put the event at the fitting keyframe, but i want to use our shooting script also for enemies. So i can control it one time with our input system, and one time controlled by our AI script for enemies.

This is how our code in our shot method currently looks.

if (rangeAttackCooldown[0] >= rangeAttackCooldown[1])
        {
            if(anim!=null)
                anim.SetTrigger("Shot");
            GameObject proj = PPS.getProjectile();
            if (proj != null)
            {
                currentProjectile = proj.GetComponent<Projectile>();
                rangeAttackCooldown[0] = 0;
                if (is_normal_shot)
                    currentProjectile.set_shooting_type(Projectile.Shooting_Type.NORMAL);
                else
                    currentProjectile.set_shooting_type(Projectile.Shooting_Type.SPECIAL);
                currentProjectile.shoot(2.0f, facingRight);
            }
        }

You have basicly 2 possibiltys.
Use the animation event system to instantiate the bullet at the right frame, or you create some offset variables and use an ienumerator to use them.

private IEnumerator ranged_attack()
	{
		is_attacking_ranged = true;
		{
			yield return new WaitForSeconds(attack_speed);
			Instantiate(Bullet, BulletSpawnPoint.position, BulletSpawnPoint.rotation);
			yield return new WaitForSeconds(attack_offset);
		}
		is_attacking_ranged = false;
	}

like this.

attackspeed is the space before the actual bullet is instantiated, attack_offset to complete the rest of the animation if there is any.

Cheers

Allright i got it.
My Code in my input system:

//Funktion für Schießen
        if (Input.GetKeyDown("s"))
        {
            StartCoroutine(actions.shoot(true));
        }

        if (Input.GetKeyDown("p"))
        {
            StartCoroutine(actions.shoot(true));
        }

My shooting Method

public IEnumerator shoot(bool is_normal_shot)
    {
        if (rangeAttackCooldown[0] >= rangeAttackCooldown[1])
        {
            if(anim!=null)
                anim.SetTrigger("Shot");
            while (!shotAnimationReady)
            {
                yield return null;
            }
            GameObject proj = PPS.getProjectile();
            shotAnimationReady = false;
        }
    }

And my “readyShot” which is called from the keyframe:

//Methode auf die der Keyframe zugreift, um den Schuss zu ermöglichen
    public void setShotAnimationReady()
    {
        shotAnimationReady = true;
    }

This does it for my problem.