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 JoshMBeyer · Apr 19, 2013 at 03:21 PM · randomtimershoot

Need help with randomize attack time. Please help!

The concept is simple. There are 5 variables, shootTime, coolDown, timer, minTime, and maxTime. The timer is constantly going up by 1 per second (I used Time.Delta), but only between "minTime" and "maxTime". so ie; 1,2,3,4 "reset" 1,2,3,4 and so on. So if "timer" is equal to "shootTime", fire a missle(i already have that code) then randomize the shootTime between the minTime and maxTime. This is kind of like space invaders attack style. That is the random attack type I am looking for. Theres is a mob of enemies all attacking at the random rates between a set amount in my case between 0 and 5.

Can anyone help me fix this code?

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAttack : MonoBehaviour {
     
     
     //------Variables Start------
     
     public         float         time;
     public         float         minTime;
     public         float         maxTime;
     public         float         shootTime;
     public         GameObject     missile;
     
     //------Variables End------
     
     
     void Start()
     {
         //Give all enemies a random shootTime to start with.
         shootTime = Random.Range(minTime,maxTime);
     }
     
     // Update is called once per frame
     void Update () 
     {
         
         //Do not allow the time to exceed the limits of minTme and maxTime.
         time = Mathf.Clamp(time,minTime,maxTime);
         
         //Add 1.0 every second.
         time += 1f  * Time.deltaTime;
         
         //If time reaches the max, reset.
         if(time >= maxTime)
         {
             time = minTime;
         }
         
         if(time >= shootTime)
         {
             //Shoot missle. Having trouble here. It instantiates like 20 at a time.
             //I think it has to do with the time >= shootTime but if i put == it doesn't work for me.
             Instantiate(missile,transform.position,transform.rotation);
             
             //Randomize the next shootime.
             shootTime = Random.Range(minTime,maxTime);
         }
     }
 }
Comment
Add comment · Show 3
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 HypoXic5665 · Apr 19, 2013 at 04:32 PM 0
Share

You may want to check out the Coroutine Documentation

You can randomize a waitTime variable that is passed into the coroutine and avoid all of the conditional checks in your Update() function.

avatar image JoshMBeyer · Apr 19, 2013 at 04:44 PM 0
Share

I tried that but i can't figure out how to make it work right. Where do I start it? If i put start coroutine in the start function it only runs once. if i put it in the update it calls it every frame. how would i do this? the examples don't really help use it for what im looking for

avatar image HypoXic5665 · Apr 19, 2013 at 05:18 PM 0
Share

I am not too familiar with java script so apologies if the syntax is wrong. But I would assume the logic would be something like this if you are looking to have a new missile fire continuously at a random interval:

 function Start(){
      StartCoroutine(Shoot$$anonymous$$issile());
 }
     
         
 function Shoot$$anonymous$$issile() {
      //do this until <StopCoroutine()> is called
      while(true){
          //create and wait for a random delay time
          var randTime:float = Random.Range($$anonymous$$Time,maxTime);
          yield WaitForSeconds(randTime);
          //make missile
          Instantiate(missile,transform.position,
             transform.rotation);
      }
 
 //call to stop the missile fire loop
 function Stop$$anonymous$$issileCycle(){
      StopCoroutine(Shoot$$anonymous$$issile());
 }

Whenever you need the $$anonymous$$issile cycle to stop just call the function Stop$$anonymous$$issileCycle() or more generically StopCoroutine(CoroutineName).

2 Replies

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

Answer by pudd1nG · Apr 19, 2013 at 03:28 PM

Here is what I've noticed in your code.

-You don't need to multiply deltaTime by 1, just add deltaTime every FixedUpdate.

-You don't need the clamp

-After your Instantiate, reset your time to 0

Let me know how you get on after those changes :)

Comment
Add comment · Show 1 · 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 JoshMBeyer · Apr 19, 2013 at 04:16 PM 0
Share

How do I do the add delta every fixed update?

avatar image
0

Answer by JoshMBeyer · Apr 19, 2013 at 04:59 PM

I think I figured it out my self by using this code.

 void Update () 
     {
         fireRate = Random.Range(minTime,maxTime);
         
         if(Time.time > nextFire)
         {    
             nextFire = Time.time + fireRate;
             
             Instantiate(missile,transform.position,transform.rotation);
         }
     }
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

13 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

Related Questions

How would you make your player freeze for a certain amount of time? 2 Answers

Make enemies spawn faster with timer? (C#) 2 Answers

Adding a timer to spawner 2 Answers

SimpelTimerScript 1 Answer

Pause for random amount of time at waypoint 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