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
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?
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.
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.