• 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 xyHeat · Mar 25, 2016 at 12:46 PM · bulletturretenemiestower-defensefiring

Turret Firing problem [C#]

Hi everybody ! I'm trying to make a turret that fire at enemies when there are in his collider. But the problem is that : InvokeRepeating don't work ... my turret fire only when a new mob enter in his collider ... I tried ifferents methods. The last was a state machine it didn't work too...

this is my script :

 using UnityEngine;
 using System.Collections;
 
 public class Tower : MonoBehaviour {
 
     private GameObject mob;
     public GameObject bullet;
     public GameObject gun;
     public int power = 100;
 
     private int State;
 
     private bool isFiring;
 
     void Start()
     {
         State = 0;
         UpdateStateMachine();
         isFiring = false;
     }
 
     void Update()
     {
         if(mob != null)
         {
             transform.LookAt(mob.transform.position);
             
         }
     }
 
 
     void OnTriggerEnter (Collider col)
     {
       if(col.tag == "MobCol")
         {
             mob = col.gameObject.transform.parent.gameObject;
             State = 1;
             UpdateStateMachine();
         }
     }
 
     void OnTriggerExit(Collider col)
     {
         State = 0;
         UpdateStateMachine();
     }
 
     public void Fire()
     {
         GameObject towerBullet = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.identity);
         towerBullet.GetComponent<Rigidbody>().AddForce(transform.forward * power);
     }
 
     public void UpdateStateMachine()
     {
         switch (State)
         {
             case 0:
                 CancelInvoke("Fire");
                 break;
             case 1:
                 InvokeRepeating("Fire", 0f, 0.5f);
                 break;
         }
     }
 
 }
 

Thank you ! Bye, xyHeat

Comment
Add comment · Show 2
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 TreyH · Mar 25, 2016 at 01:10 PM 0
Share

so it only shoots once at the new mob, or only fires at that single mob repeatedly?

Show more comments

2 Replies

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

Answer by TreyH · Mar 25, 2016 at 04:51 PM

Try this and see what happens:

 using UnityEngine;
 using System.Collections;
 
 public class Tower : MonoBehaviour {
     
     private GameObject mob;
     public GameObject bullet;
     public GameObject gun;
     public int power = 100;
 
     private bool isFiring;
     private IEnumerator fireRoutine;
 
     void Update()
     {
         if(mob != null)
         {
             transform.LookAt(mob.transform.position);
         }
     }
     
     
     void OnTriggerEnter (Collider col)
     {
         if(col.tag == "MobCol")
         {
             mob = col.gameObject;
             fireRoutine = FireRoutine();
             StartCoroutine(fireRoutine);
         }
     }
     
     void OnTriggerExit(Collider col)
     {
         // Make sure our initial mob should be removed
         if(col.gameObject == mob)
         {
             if (fireRoutine != null)
             {
                 StopCoroutine(fireRoutine);
             }
 
             mob = null;
         }
     }
 
     public IEnumerator FireRoutine()
     {
         // Use a Coroutine for this
         WaitForSeconds wait = new WaitForSeconds(0.2f);
 
         // Keep ourselves from firing at nothing
         while (mob != null)
         {
             Fire();
 
             // Return our yield instance
             yield return wait;
         }
     }
 
     public void Fire()
     {
         GameObject towerBullet = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.identity);
         towerBullet.GetComponent<Rigidbody>().AddForce(transform.forward * power);
     }
     
 }
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 xyHeat · Mar 25, 2016 at 04:59 PM 0
Share

Thank you, it work, i don't know why but i never use coroutine... Thank you again ! Bye, xyHeat

avatar image TreyH xyHeat · Mar 25, 2016 at 05:01 PM 0
Share

glad to help, keep in $$anonymous$$d that this will stop firing if two enemies enter and the active enemy leaves. You may want to adjust your script to fire at a List of enemies, or something similar.

avatar image
0

Answer by Carlz · Mar 25, 2016 at 05:54 PM

Add a reference of every enemy that has entered the collider to a List and let the turret look only at the first Object in the List. Then start a coroutine that fires a projectile every x seconds to the position of this first item in the List. When this Item is destroyed you will have to update the list and a new enemy will be on position 1. I didn't test this but that is the way I would do it.

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 xyHeat · Mar 26, 2016 at 10:55 AM 0
Share

I'm looking for it !

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Turret bullet rotation problem 1 Answer

My turret is a pacifist. 2 Answers

Turret shooting arrows? 1 Answer

NullReferenceException with a turret shooting a bullet 2 Answers

How to stop turret firing? 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