Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hodoer · May 23, 2020 at 01:59 PM · animatorbulletshmup

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

  1. AI moves in a certain pre-determined pattern on screen

  2. Randomize choices of patterns

  3. Activate pre-determined bullet pattern for assigned movement pattern

  4. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet Pattern for Bullet Hell 1 Answer

How do I create bullet patterns (multiple bullets, different world positions) 3 Answers

Ship won't shoot right in SMHUP. Help?? 2 Answers

2D Animation does not start 1 Answer

how to rotate to face camera point in 2D 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges