- Home /
SHMUP Boss, changing firing rate
HI,
I'm making a SHMUP game and I encounter this issue where I cannot change the rate of fire resulting in my screen flooded with projectiles, making the game unplayable. I'm using the animator to create a simple AI for the boss movement, moving from point to point on screen. Here is the script for one of the sequences:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss001_003_P1 : StateMachineBehaviour
{
public float speed = 30f;
Transform wp;
Rigidbody rigidbody;
BulletPattern003a bp;
public float rateOfFire = 2f;
public float angleIncrement = 20f;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.SetBool("Next State", false);
wp = GameObject.FindGameObjectWithTag("WP3").transform;
rigidbody = animator.GetComponent<Rigidbody>();
bp = animator.GetComponent<BulletPattern003a>();
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
bp.Start();
bp.rateOfFire = rateOfFire;
bp.angleIncrement = angleIncrement;
Vector3 target = new Vector3(wp.position.x, 0f, wp.position.z);
Vector3 newPos = Vector3.MoveTowards(rigidbody.position, target, speed * Time.deltaTime);
rigidbody.MovePosition(newPos);
if (rigidbody.transform.position == target)
{
animator.SetBool("Next State", true);
}
else
{
animator.SetBool("Next State", false);
}
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
bp.CancelInvoke();
}
}
Here I called out the bullet pattern on the OnStateEnter. This is the script for the bullet pattern:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletPattern003a : MonoBehaviour
{
private float angle = 0f;
public float angleIncrement = 10f;
public float rateOfFire = 1f;
public int numberOfSpirals = 1;
public void Start()
{
InvokeRepeating("Fire", 0f, rateOfFire);
}
private void Fire()
{
float spreadAngle = 360f - (360f / numberOfSpirals);
for (int i = 0; i <= (numberOfSpirals - 1); i++)
{
float proDirX = transform.position.x + Mathf.Cos(((angle + spreadAngle * i) * Mathf.PI) / 180f);
float proDirZ = transform.position.z + Mathf.Sin(((angle + spreadAngle * i) * Mathf.PI) / 180f);
Vector3 proMoveVector = new Vector3(proDirX, 0f, proDirZ);
Vector3 proDir = (proMoveVector - transform.position).normalized;
GameObject pro = ProjectilePool.projectilePoolInstances.GetProjectile();
pro.transform.position = transform.position;
pro.transform.rotation = transform.rotation;
pro.SetActive(true);
pro.GetComponent<EnemyProjectileProperties>().SetProjectileDirection(proDir);
}
angle += angleIncrement;
if (angle >= 360)
{
angle = 0f;
}
}
}
When testing the bullet pattern on its own, I can manipulate all of its variables. However, when implemented onto the animator, I cannot control the rate of fire. Changing the angle of increment works fine but nothing seems to work for the rate of fire. Is implementing Start(); in the animator the wrong move? Or am I missing any steps in between? Please advise. Thank you.
Answer by Hodoer · May 28, 2020 at 01:02 PM
Hi again,
I've tried several method but to no avail. The animation just won't respect the rate of fire I've set either in the inspector or hard-coding it in the script. Is there a better approach to achieving these objective in a SHMUP:
AI moves in a certain pre-determined pattern on screen
Randomize choices of patterns
Activate pre-determined bullet pattern for assigned movement pattern
Most importantly - control the rate of fire for the bullets
I've achieved the first 3 objects through the use of animator, but I'm having a lot of trouble with the final objective. Please help. Thank you.