Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sawula · Dec 27, 2014 at 04:46 PM · randomenemyfirerate

Random enemy fire rate

Hello guys, I'm trying to make a school project (space invader) and I have a itty problem with my alien shooting

I want them to shoot at a random rate, I more o less managed to do that I think... Problem is I don't know why, at the beginning of the game they all shoot once, all together. How do I make the random shooting start at the same time the game does?

Second, sometime the random make too many bullet pop at once. Is there a way to limit the possible number of alien bullets?

script is this

 using UnityEngine;
 using System.Collections;
 
 public class canonenemy : MonoBehaviour {
     public GameObject bolt;
     public Transform Canon;
     public float fireRatep;
     public float fireRatem;
     private float nextFire = 3.0f;

     // Update is called once per frame
     void FixedUpdate () {
         if (Time.time > nextFire) {
             nextFire = Time.time + Random.Range (fireRatep, fireRatem);
         Instantiate (bolt, Canon.position, Canon.rotation);
         }
     }
 }

Thanks in advance! ^^

Comment
Add comment · Show 1
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
avatar image Sawula · Dec 28, 2014 at 02:15 PM 0
Share

Ok, thanks a lot, my first problem is now solved. The second one, I explain better, is that I'd like to have a certain number of enemy bullet on screen at the same time.

Like no more than 5 enemy bullets, present at the same time, but I have no idea how to tell Unity to do that...

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BabilinApps · Dec 27, 2014 at 07:58 PM

It's because the value of

 `private float nextFire = 3.0f;`

starts at 3 for all of the enemies. Try adding:

  `nextFire = nextFire + Random.Range (fireRatep, fireRatem);`

to your start function to offset all of the start times by the random range.

Best luck to 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
avatar image
0

Answer by Steffen D E · Dec 27, 2014 at 07:58 PM

I think your problems lie in FixedUpdate and in next fire.
FixedUpdate is called independetly of framerate, so if you use Update instead, I think that should fix the douple fireing.
For everyone firing at one, is because they are all set to fire 3 sec. after start (nextFire = 3).
you can fix this by using:

 void Start()
 {
    nextFire = Time.time + Random.Range (fireRatep, fireRatem);
 }

hope this fixes it :)

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
avatar image
0

Answer by troien · Dec 27, 2014 at 05:14 PM

The reason why hey fire at the same time at the start is because you set nextfire to a fixed value at the start (3.0f), and then after they fired once, you set it to a random value.

You probably want to set it to a random value at the start. Like this:

 private void Start()
 {
     nextFire = Time.time + Random.Range(fireRatep, fireRatem);
 }

Your second problem is a bit vague. Do you mean that one cannon spaws its bullets to fast after each other sometimes? (If so, increase fireRatep. As this is the minimum time in secs between spawning bullets, while fireRatem is the maximum time in seconds between the spawning of bullets)

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
avatar image
0

Answer by chillandcodegames · Aug 04, 2021 at 08:14 AM

This is what I have used and it works awesome.

  fireInterval = Random.Range(1,20);
         if (playerAlive){
             if (Time.time > nextFire)
             {
                 shoot();
                 nextFire += fireInterval/3;
             }
         }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with enemy respawn script 1 Answer

2d enemy prefab (enemy movement and instantiate) 1 Answer

How can i make my enemy walk random in my level? 1 Answer

Endless/infinite runner random enemy spawning multi lane 1 Answer

Enemy moves randomly within a area. 1 Answer


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