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 /
  • Help Room /
avatar image
0
Question by testlabs · May 20, 2017 at 01:59 PM · instantiateprojectileinterval

Instantiate projectiles at intervals

Hey there, I'm trying to get a turret to fire projectiles when it detects the player. All this works but I want it to fire slower as the projectiles are spawned so quickly that they destroy each other once I turn on their collider triggers. Here's the code attached to the turret:

public class staticEnemy : MonoBehaviour {

 public Transform staticAiFirePoint;
 public GameObject upBullet;
 public Transform playerCheck;
 public float playerCheckRadius;
 private bool playerDetected;
 public LayerMask whatIsPlayer;

 // Use this for initialization
 void Start () {


 }

 // Update is called once per frame
 void Update () {

     playerDetected = Physics2D.OverlapCircle (playerCheck.position, playerCheckRadius, whatIsPlayer);

     if (playerDetected)
         Instantiate (upBullet, staticAiFirePoint.position, staticAiFirePoint.rotation);

 }

}

How and where do I stick in a command for intervals? 1 bullet per second would be great.

Also, on my bullets when I use public float speed I can get them to fire left or right with + or -, I'm curious as to how to get them to go up or down. Right now I'm getting the projectiles in question to go up using this:

GetComponent ().velocity = new Vector2 (0f, 15f);

Thanks for looking at my question and I hope to hear from you

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

2 Replies

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

Answer by Jawchewa · May 20, 2017 at 08:26 PM

My recommendation would be to use a Coroutine to accomplish this instead of calling instantiate in update. This would allow you to time it to fire at every interval, instead of every frame. That would look something like this:

 IEnumerator ShootTarget()
     {
         yield return new WaitForSeconds(1.0f);
         if (playerDetected)
             Instantiate(upBullet, staticAiFirePoint.position, staticAiFirePoint.rotation);
         StartCoroutine(ShootTarget());
     }
     void Start()
     {
         StartCoroutine(ShootTarget());
     }
     void Update()
     {
         playerDetected = Physics2D.OverlapCircle(playerCheck.position, playerCheckRadius, whatIsPlayer);
     }

You can change the value in the WaitForSeconds function to change the interval. I would also recommend looking more into how Coroutines work to make sure that you fully understand it.

As far as your projectiles problem goes, I'm not sure that I fully understand your question. The way you are setting the velocity is a valid way to send it in a direction, where the first value of the vector is the x velocity, and the second value is the y. Could you elaborate more on what your problem is?

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
0

Answer by testlabs · May 20, 2017 at 08:40 PM

Thank you for the nice answer. Here's how I ended up getting the intervals

 public Transform staticAiFirePoint2;
 public GameObject downBullet;
 public Transform playerCheck;
 public float playerCheckRadius;
 private bool playerDetected;
 public LayerMask whatIsPlayer;

 // Use this for initialization
 void Start () {
     
     InvokeRepeating ("Fire", 1f, .8f);
 }

 // Update is called once per frame
 void Fire () {

     playerDetected = Physics2D.OverlapCircle (playerCheck.position, playerCheckRadius, whatIsPlayer);

     if (playerDetected)
         Instantiate (downBullet, staticAiFirePoint2.position, staticAiFirePoint2.rotation);

 }

 void OnDestroy()
 {
     scoreManager.AddPoints (500);
 }

}

but your post gives me reason to look into coroutines as you suggest and thanks again for that.

Here's what I use for projectiles going left to right after floating speed:

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
     

Then I needed projectiles to fire up or down so I changed the velocity y to x but it didn't work, they still went left/right so I used this to get it to work:

GetComponent ().velocity = new Vector2 (0f, 15f);

I'm just curious as to why the former didn't work as I expected it to. I'm also curious as to how to make a projectile fire at the direction my player is moving or at the players current position.

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 Jawchewa · May 20, 2017 at 09:00 PM 0
Share

I would assume that changing it to this would solve your problem of making it move upwards:

 GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, speed);

So, if that doesn't work, I don't know what the problem would be, as I would think that should work, but it kind of sounds like you tried that already, so I don't know.

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

118 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

Related Questions

Help I use rigidbody.addforce to fire projectiles an it randomly doesn't go to the crosshair! 0 Answers

Problem with projectile direction 0 Answers

How do I bring an instantiated projectile back towards the player if the player tells it to? Similar to a boomerang. 0 Answers

enemy shoots faster as it gets closer to player 1 Answer

How to make an object shoot a projectile? 4 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