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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Higuy7788 · Apr 26, 2015 at 11:15 PM · instantiatetower-defense

Tower Defense Game Shooting Script Troubles

i have the following code:

 void OnTriggerStay(Collider co)
     {
         if (co.GetComponent<Monster> ())
         {
             StartCoroutine(shoot());
             mon = co.transform;
         }
     }
 
     IEnumerator shoot(){
         yield return new WaitForSeconds (1);
         GameObject g = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.identity);
         g.GetComponent<Bullet> ().target = mon;
     }


This is so that once an enemy walks into the trigger on my tower it starts shooting the monster. the current script makes many bullets shoot at one time and im not sure why because i added the "waitforseconds" any help would be greatly appreciated and as you i may be able to tell i am a begginer

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 DoTA_KAMIKADzE · Apr 26, 2015 at 11:22 PM

Well that is because you Start so many coroutines (1 each time OnTriggerStay is firing). With that code you should have initial delay of 1 sec and then massively fire your monster.

The easies way to fix that would be something like that:

 private bool firing = false;
 void OnTriggerStay(Collider co)
 {
     if (co.GetComponent<Monster>() && !firing)
     {
         firing = true;
         StartCoroutine(shoot());
         mon = co.transform;
     }
 }
 IEnumerator shoot()
 {
     yield return new WaitForSeconds(1);
     GameObject g = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
     g.GetComponent<Bullet>().target = mon;
     firing = false;
 }

If you don't like that approach then you can also make a cooldown with deltatime instead - check my answer HERE for an example.

P.S. The approach also highly depends on your logic of monster targeting (e.g. distance to tower or entrance turn, etc.). If you have some complex logic then you might want to manage everything yourself using OnTriggerEnter/Exit as well, but that's the whole different story.

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 Higuy7788 · Apr 26, 2015 at 11:44 PM 0
Share

Thank you so much! I tried doing something like that but I couldnt quite figure it out.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Removing all instantiated objects in scene 1 Answer

Bullet instantiates sometimes in wrong posiiton 0 Answers

Instantiate Reference Problem 1 Answer

Unet NetworkServer.Spawn() not working 5 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