
マウスを押している間は弾を連射するようにして、その間隔を調節してみます。
弾を連射するには前の記事のスクリプトのInput.GetMouseButtonDownをInput.GetMouseButtonに変えるだけです。
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// 攻撃可能な時にマウスクリックで攻撃する
if (Input.GetMouseButton(0) && attackable)
{
animator.SetTrigger("Attack"); // 攻撃ステートへ
ttackable = false; // 攻撃不可能にする
if (animator.GetInteger("Weapon") == 5) // 武器が銃のとき
{
var b = Instantiate(bullet, player.position + player.forward, player.rotation); // 銃弾を生成
Rigidbody r = b.GetComponent<Rigidbody>(); // 銃弾のRigidbody
r.AddForce(Vector3.Lerp(player.forward, player.up, 0.02f) * 38f, ForceMode.Impulse); // 銃弾を発射
}
}
}
弾の連射速度を変えるには、アニメーターコントローラーで攻撃ステートのExit TimeやTransition Durationを小さくして、弾を発射した後すぐに遷移が始まるようにしたり、素早く遷移するようにします。冒頭のGIF画像ではExit Timeが0.8です。



Exit Timeを0.5にすると連射速度が上がりましたが、さらに小さくするとかえって遅くなりました。Exit Timeで指定した時点に到達した最初のフレーム以外ではfalseになるのが原因かもしれません。
攻撃ステートへの遷移のTransition Durationを0にすると問題なく連射速度が上がりました。遷移中にExit Timeに達しても遷移せず、通り過ぎてしまうのだと思います。
