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 newb_quest · Nov 19, 2013 at 01:35 AM · ontriggerenterinvokeinvokerepeating

InvokeRepeating repeating too often with OnTriggerEnter

hey all,

Two unit objects (shooting butterflies in this case) both have sphere colliders with isTrigger checked. Let's call them BF A and BF B.

When BF B collides with the sphere trigger for BF A, BF A runs some code to instantiate a homing missile aimed at BF B and send it towards it.

I am using Invoke Repeating to allow BF A to repeatedly instantiate projectiles at BF B until B is destroyed or exits the trigger sphere.

I am using the time and repeatRate variables in InvokeRepeating to try to control the fire rate. Instead, the unit firing fires off many, many projectiles in rapid succession. I am assuming that InvokeRepeating is getting restarted due to the trigger collision but am not sure quite how that's happening. I am managing the collisions with layers so I don't think there is unwanted collision happening but that might be an issue. Finally, here is the code for the shooter:

 public var projectile:Transform;
 public var projectileSpeed:float = 0.0;
 public var delay:float = 0.0;
 public var fireRate:float = 1.0;
 public var myLayer: int; 
 public var myColorTag: String; 
 public var alreadyFiring: boolean;
 
 //private var projectileCount:int = 0;
  
 // Current player variable
 private var target:GameObject = null;
  
 function OnTriggerEnter(other:Collider)
 {
     if(other.gameObject.tag != myColorTag && other.gameObject.tag != "environment" && other.gameObject.tag != "explosion" && other.gameObject.tag != "Resource" && other.gameObject.tag != "projectile")
     {  
         if (alreadyFiring == true)
 
         { CancelInvoke("Shoot"); alreadyFiring = false; } 
         
         if (alreadyFiring == false) {
 
         InvokeRepeating("Shoot", delay, fireRate);
 
         }
     
         // Store current player
         // NB this assume that you have only one player because it will overwrite second player
         target = other.gameObject;
         InvokeRepeating("Shoot", delay, fireRate); 
         
     }
 }
  
 function OnTriggerExit(other:Collider)
 {
     if(other.gameObject.tag != myColorTag && other.gameObject.tag != "environment" && other.gameObject.tag != "explosion" && other.gameObject.tag != "Resource" && other.gameObject.tag != "projectile")
   //   print ("tested true for tag to shoot. mycolortag: " + myColorTag);
     
     {
         // Remove reference
         target = null;
         CancelInvoke("Shoot");
     }
 }
  
 function Shoot()
 { 
     alreadyFiring = true;
     var shootClone = Instantiate(projectile, transform.position, transform.rotation);
     // Set projectile velocity
     shootClone.gameObject.layer = myLayer; 
     
     var setLayerScript = shootClone.GetComponent(spawnDamageExplosion);
     if(setLayerScript) setLayerScript.myLayerForExplosion = myLayer;
 //    print ("spawned missile and initialized explosion layer to" + myLayer);
     
    
     shootClone.rigidbody.velocity = (target.transform.position - transform.position).normalized * projectileSpeed;
 }
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
Best Answer

Answer by Sparrowfc · Nov 19, 2013 at 06:42 AM

The problem is InvokeRepeating isn't getting restarted due to the trigger collision. Instead, it 'instance a new function call' once u call it. Line 31 in your script cause u invoke a new 'function instance' each time on trigger enter. That's why your script firing so many missles.

Usually I used StartCoroutine instead of InvokeRepeating to avoid the situation that I forgot to cancel the invoke or invoke a method multiple times. such as

 IEnumerator StarFireMissle(float timeInterval)
 {
 while(firing)
 {
 FireTarget();
 yield return new WaitForSeconds(timeInterval);
 }
 }
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 newb_quest · Nov 19, 2013 at 12:38 PM 0
Share

Thank you! I had an intuition that I might be creating many copies of the function. I've used startcoroutine before and will rewrite with that. Appreciate your taking the time!

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# Instant OnTriggerEnter Detection 0 Answers

A node in a childnode? 1 Answer

Player lives script help 1 Answer

Access to the script variable from a script. Javascript 0 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