Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by nitro0198 · Jul 10, 2021 at 01:10 PM · instantiateattack

How to make the enemy fire a few bullets and make him stop for a few seconds to reload

Hi, I have a simple patrol script for enemies, they just patrol an area and when the player enters their range they start firing. The problem, however, is that they shoot bullets endlessly, what I would like to do is to fire only 3 bullets and then wait a few seconds, and then continue firing, how could I do?

I also leave attached the script I am using

{ public float walkSpeed; public float range;
public float timeBTWshots; float distToPlayer; public float bulletSpeed;

 bool mustPatrol;  
 public Rigidbody2D rb;
 private bool mustTurn, canShoot;   
 public Transform groundCheckpos;
 public LayerMask groundLayer;

 public Collider2D bodyCollider;
 private Animator anim;

 public Transform player;
 public GameObject Bullet;
 public Transform shootPos;







 void Start()
 {
     mustPatrol = true;
     canShoot = true;
     anim = GetComponent<Animator>();
 }

 void Update()
 {
     if (mustPatrol)    
     {
         Patrol();
         anim.SetBool("runshoot", true);
     }

     distToPlayer = Vector2.Distance(transform.position, player.position);
    
     if (distToPlayer <= range)
     {
         if (player.position.x > transform.position.x && transform.localScale.x < 0||player.position.x<transform.position.x && transform.localScale.x>0)
         {
             Flip();
         }

         mustPatrol = false;
         
         if (mustPatrol == false)
        
         {
             anim.SetBool("runshoot", false);
             rb.velocity = Vector2.zero;
             anim.SetTrigger("shootenemy");
         }

         if(canShoot)
         StartCoroutine(Shoot());
      


      
     }
    
     else 
     {
         mustPatrol = true;
     }

 }

 void FixedUpdate()
 {
     if (mustPatrol)
     {
         mustTurn = !Physics2D.OverlapCircle(groundCheckpos.position, 0.1f, groundLayer);
     }
 }
 void Patrol()
 {
     if (mustTurn||bodyCollider.IsTouchingLayers(groundLayer))
     {
         Flip();
     }
     rb.velocity = new Vector2(walkSpeed * Time.fixedDeltaTime, rb.velocity.y);  
 }
   
 void Flip() 
 {
     mustPatrol = false;
     transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
     walkSpeed *= -1;
     mustPatrol = true;
 }

 IEnumerator  Shoot()
 {
     canShoot = false;

     yield return new WaitForSeconds(timeBTWshots);
     GameObject newBullet = Instantiate(Bullet, shootPos.position, Quaternion.identity);

     newBullet.GetComponent<Rigidbody2D>().velocity = new Vector2(bulletSpeed * walkSpeed * Time.fixedDeltaTime, 0f);

     canShoot = true;

    
 }


}

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

0 Replies

· Add your reply
  • Sort: 

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

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

Why my object dont want to Update ? 0 Answers

Prefab not Instantiating 0 Answers

Instantiating GameObject loses some properties of the original GameObject 1 Answer

Instantiate object at mouse position 0 Answers

How does one instantiate a prefab with velocity? 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