Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 qwert362rose · Mar 20, 2018 at 09:43 PM · timeshootingenemyairate

How do I set a fire rate for my enemy shooting?

Hello! I'm working on a 2D platform game and I'm having trouble setting time restrictions for my rate of shooting. I would like a bullet to be shot out by the enemy every 1 second but instead I get a continuous stream of bullets from the enemy when player is in range. How can I fix this?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class enemy_move : MonoBehaviour {

 //Moving
 public float MoveSpeed;
 public int MaxDist;

 //Shooting
 public Rigidbody2D bulletPrefab;
 public Transform LSpawn;
 public Transform RSpawn;
 public Vector2 speed;
 Rigidbody2D rb2d;

 //All
 public Transform target;
 public int radius;
 bool inRadius;


 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
     rb2d.velocity = speed;
 }

 void Update()
 {
     EnemyMove();

     if (inRadius)
     {
         EnemyShoot();
     }

 }

 public void EnemyMove()
 {
     float distFromTarget = Vector2.Distance(transform.position, target.position);

     if (distFromTarget <= radius)
     {
         inRadius = true;

     }
     else
     {
         inRadius = false;
     }

     //MOVES ENEMY TOWARDS TARGET
     if (inRadius && distFromTarget > MaxDist)
     {
         transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed);
     }
 }

 public void EnemyShoot()
 {
         float DistfromSpawnL = Vector2.Distance(LSpawn.position, target.position);
         float DistfromSpawnR = Vector2.Distance(RSpawn.position, target.position);

         if (DistfromSpawnL < DistfromSpawnR )
         {
             Rigidbody2D BulletInstance = Instantiate(bulletPrefab, LSpawn.position, LSpawn.rotation);
             BulletInstance.velocity = speed;
         }

         if (DistfromSpawnR < DistfromSpawnL)
         {

             Rigidbody2D BulletInstance = Instantiate(bulletPrefab, RSpawn.position, RSpawn.rotation);

             BulletInstance.velocity = speed;

         }
     }

 }

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

Answer by Rexro · Mar 21, 2018 at 02:33 AM

You can add a public float, I will call it time. At the top of the Update() you can add this in:

     public float time;
     private void Update()
     {
         time += Time.deltaTime;
         if(time >= 1)
         {
             time = 0;
             if (inRadius)
             {
                 EnemyShoot();
             }
         }
     }

Time.deltalTime is the time between the function being called last. So adding it to time will show the time in seconds from the last time it was set to 0. The if statement will check if the time passed is greater or equal to 1 second, and if so, your function will be called, and time is set back to 0 to restart the process.

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

77 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 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

Rate of health loss in trigger box. 1 Answer

Enemy shooting player driving me crazy! 2 Answers

Enemy shoot at player in 2D game 3 Answers

trying to regulate character speed. 1 Answer

Enemy AI problem 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