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
1
Question by onionman99 · Dec 12, 2013 at 01:26 AM · instantiatebulletdelay

how to make an enemy fire a bullet once every second

i have a very basic enemy AI script to make the enemy follow me and when within range shoot at me but it continuously fires bullets extremely fast. i want it to fire bullets much slower than it does. any way i can instantiate the bullet every second instead of constantly?

my AI code is:

 var bullet      : Transform;
 var bulletSpawn : Transform;
 
  
  
 
 function Update () 
 {
     transform.LookAt(Player);
  
     if(Vector3.Distance(transform.position,Player.position) >= MinDist){
  
          transform.position += transform.forward*MoveSpeed*Time.deltaTime;
  
  
  
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
              {
                Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
    } 
  
    }
 }
Comment
Add comment · Show 1
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 robertbu · Dec 12, 2013 at 01:27 AM 0
Share

There are several ways to make this happen. In order to get an accurate and well targeted answer, we need to see your code. Please edit your question and add your shooting code.

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by TargonStudios · Dec 12, 2013 at 01:32 AM

You could add in a simple variable. For example, you could add in a variable "CanShoot", once the enemy has shot, set it to false. Then call on a reload function, after a few seconds, make it true again.(Although, without your script, its harder to answer. You should always post your script along with your question) Example:

 var CanShoot = true;
 
 function Update(){
 if(CanShoot == true){
 //InRange scripts
 //Shot scripts
 CanShoot = false;
 Reload();
 }
 }
 
 function Reload(){
 yield WaitForSeconds(1);
 CanShoot = true;
 }

Hope this helps!

Comment
Add comment · Show 6 · 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 ThiagoTejo · Dec 12, 2013 at 01:33 AM 0
Share

It's better to use InvokeRepeating i think, it's simple and easier in general.

avatar image onionman99 · Dec 12, 2013 at 01:34 AM 0
Share

thanks, this actually helps a lot

avatar image TargonStudios · Dec 12, 2013 at 01:36 AM 0
Share

I prefer this way because I can use it in both player Guns, and NPC guns.(I like keeping things the same)

avatar image TargonStudios · Dec 12, 2013 at 01:41 AM 0
Share

If it answered your question, why not mark it as correct? ;D

avatar image Melvin8D · Oct 19, 2014 at 06:54 PM 0
Share

is there a way I could maybe remove the person looking at you?

Show more comments
avatar image
2

Answer by ThiagoTejo · Dec 12, 2013 at 01:33 AM

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html

Take a look at this. Its very simple, just put your firing code in a function, and use InvokeRepeating with 1 second time. The exemple in the docs is basically what you're looking for.

Also, don't use Invoke Repeating in the Update function. use it in Start or other function you have.

using in the Update function will have it starting every frame, and that's not you're looking for.

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

Answer by robertbu · Dec 12, 2013 at 01:42 AM

The easiest solution is to use a timestamp:

 var bullet      : Transform;
 var bulletSpawn : Transform;
 var timeBetweenShots = 1.0;
 
 private var timestamp = 0.0;
  
 function Update ()  {
     transform.LookAt(Player);
  
     if(Vector3.Distance(transform.position,Player.position) >= MinDist) {
         transform.position += transform.forward*MoveSpeed*Time.deltaTime;
  
         if(Vector3.Distance(transform.position,Player.position) <= MaxDist && (Time.time > timestamp)) {
             Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
             timestamp = Time.time + timestamp;
         } 
     }
 }
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 Solber · Aug 01, 2020 at 03:55 PM 0
Share

Small mistake it must be

  var bullet      : Transform;
  var bulletSpawn : Transform;
  var timeBetweenShots = 1.0;
  
  private var timestamp = 0.0;
   
  function Update ()  {
      transform.LookAt(Player);
   
      if(Vector3.Distance(transform.position,Player.position) >= $$anonymous$$inDist) {
          transform.position += transform.forward*$$anonymous$$oveSpeed*Time.deltaTime;
   
          if(Vector3.Distance(transform.position,Player.position) <= $$anonymous$$axDist && (Time.time > timestamp)) {
              Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
              timestamp = Time.time + timeBetweenShots;
          } 
      }
  }
avatar image
0

Answer by Solber · Aug 01, 2020 at 03:56 PM

Here is my working C# code :

 float distanceToPlayer = Vector3.Distance(transform.position, other.transform.position);
         if (hitDist < distanceToPlayer && moveToPlayer)
         {
             transform.position = Vector2.MoveTowards(
                 transform.position,
                 other.transform.position,
                 MoveSpeed * Time.deltaTime);
         }
         else if (hitDist >= distanceToPlayer)
         {
             moveToPlayer = false;
             if (canFire)
             {
                 canFire = false;
                 timestamp = Time.time + 1.0f;
                 hitPlayer();
             }
             else if (Time.time > timestamp)
             {
                 canFire = true;
             }
         }

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

20 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

Related Questions

Instantiating a pattern in front of the player? 1 Answer

Instantiated bullets hitting GameObjects? 4 Answers

How to check who shot whom 3 Answers

Shot Delay between button press. c# 2 Answers

Turret bullet rotation 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