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 phant0mm · Jan 03, 2013 at 06:01 PM · spherefireinterval

Fire time interval problem?

Hello everyone. I have a problem. In my 2d maze game i created four bomb machine. These machines should fire bomps (Sphere) fixed time interval. (but each one is different time. Ex: first machine each two sec., second is 1sec etc.). I can do it for one sphere. But when i put sphere each machine and set fire rate and dead time on the inspector, all fire times start to run wrong and badly. This is my code:

 var projectile : Transform;
 
 var bulletSpeed : float = 20;
 
 var destroy: float =5.0;
 
 private var nextFireTime : float = 0.0;
 
 private var fireRate : float = 1.0;
 
 
 function Update () {
 
  
     if (Time.time - fireRate > nextFireTime)
 
         nextFireTime = Time.time - Time.deltaTime;
 
  
         while(nextFireTime < Time.time){
 
             fireProjectile();
             
             WaitAndDestroy();
 
             nextFireTime = Time.time + fireRate;
 
         }   
 }
 
  
 
 function fireProjectile(){
 
     var clone : Transform;
 
     clone = Instantiate(projectile, transform.position, transform.rotation);
 
     clone.rigidbody.AddForce(clone.transform.forward *  bulletSpeed);
 
 }
 
 function WaitAndDestroy()
 {
   yield WaitForSeconds(destroy);
   
   Destroy(GameObject.Find("Roll A Ball(Clone)"));
 }
     

Also i put spheres projectile' roll a ball. I tried to create four different script four different roll a ball for sphere. But again i can not do it. But code run for one fire correctly. Please help me about this problem.

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
1

Answer by Zarenityx · Jan 03, 2013 at 08:47 PM

It is glitching probably because you are using GameObject.find(), which works fine when there is only one object of that name, but can cause problems when there are more than one. If you destroy the clone variable, instead of the return of the Find() function, it should work fine, because clone is specific to the script, whereas there may be multiple "Roll A Ball(Clone)" instances. And the yield is unnecessary, as the time can simply be added as a second parameter for Destroy(). Also,a lot of games wait until the projectile is destroyed for bomb-like projectiles,rather than a fixed firerate, because it is easier on performance and will make sure there are only as many bombs as there are launchers at one time or another, so there is no chance of overpopulating the scene with bombs and possibly crashing the game with an access violation error. I would have created it this way:

 /* Add a smoothlookat component to your launcher and set it to target the player, then add this javascript to it. */
 #pragma strict
 var projectile : GameObject;
 var initForce : float;
 private var current : GameObject;
 function Update(){
 if(current == null){
 current=Instantiate(projectile,transform.position,transform.rotation);
 current.rigidbody.velocity = Vector3(0,0,initForce);
 }
 }

And

 //Put this on the bullet, as well as a rigidbody.
 var MinLife : float;
 var MaxLife : float;
 var RemainTime : float;
 var Explosion : GameObject;
 private var lifetime : float;
 function Awake(){
 lifetime = Random.Range(MinLife,MaxLife);
 StartDeath();
 }
 function StartDeath(){
 yield WaitForSeconds(lifetime);
 Instantiate(Explosion,transform.position,transform.rotation);
 Destroy(gameObject,RemainTime);
 

Note that you will need multiple bullet types for my method, but you may find it faster and more efficient.

Comment
Add comment · Show 2 · 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 phant0mm · Jan 03, 2013 at 09:01 PM 0
Share

Thank you. I solved my problem with your help.

avatar image Zarenityx · Jan 08, 2013 at 10:55 PM 0
Share

Your welcome. Did you end up using the manual and constant firerate, or the automatic firerate? Just wondering.

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

9 People are following this question.

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

Related Questions

How do i get world position of collider if it inside multiple scaled gameobjects? 0 Answers

Sphere control 1 Answer

Neighboring grid slots on a cube sphere? 0 Answers

Cube starts shaking when placed on a giant sphere 1 Answer

Instantiate bullet in time interval for "rifle". 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