Hey guys,
struggling with this, I know it’s something simple but I can’t work it out (If it’s not clear, I’m not very well versed with C#, so apologies)
so simply, we have a turret that’s rotating back and forth, the number of bullets it will shoot depends on the value of the ballsValue float from another script.
It works, but it spurts all of the balls out pretty much at once, or at least far too fast.
My thinking was if I could put it in a coroutine and add a Waitforseconds at the end of the for loop, it will wait that amount of time then loop back through, giving me a gap of 0.25 seconds between each shot.
This doesn’t work, all that happens is it just shoots one ball regardless of how many it’s told to shoot earlier.
probably a simple obvious fix, but can someone help?
IEnumerator ReleaseBallCoroutine()
{
for (int i = 0; i < ballSpawner.ballsValue; i++)
{
GameObject bulletClone = Instantiate(bullet, bulletSpawner.transform.position, bulletSpawner.transform.rotation);
Rigidbody bulletRB = bulletClone.GetComponent<Rigidbody>();
bulletRB.AddForce(-bulletSpawner.transform.forward * m_Power, ForceMode.Force);
yield return new WaitForSeconds(0.25f);
}
}
EDIT: Here’s the whole script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReleaseScript : MonoBehaviour
{
public GameObject bullet;
public GameObject bulletSpawner;
Rigidbody m_Rigidbody;
public float m_Power = 5000f;
void Start()
{
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ball")
{
print("Balls Released");
Destroy(col.gameObject);
//ReleaseBalls();
StartCoroutine(ReleaseBallCoroutine());
ballSpawner.ballsValue = 1;
}
}
void Update()
{
if(Input.GetKeyDown("f"))
{
//ReleaseBalls();
StartCoroutine(ReleaseBallCoroutine());
}
}
// void ReleaseBalls()
// {
// for (int i = 0; i < ballSpawner.ballsValue; i++)
// {
// GameObject bulletClone = Instantiate(bullet, bulletSpawner.transform.position, bulletSpawner.transform.rotation);
// Rigidbody bulletRB = bulletClone.GetComponent<Rigidbody>();
// bulletRB.AddForce(-bulletSpawner.transform.forward * m_Power, ForceMode.Force);
// print("Relesing");
// }
// }
IEnumerator ReleaseBallCoroutine()
{
for (int i = 0; i < ballSpawner.ballsValue; i++)
{
GameObject bulletClone = Instantiate(bullet, bulletSpawner.transform.position, bulletSpawner.transform.rotation);
Rigidbody bulletRB = bulletClone.GetComponent<Rigidbody>();
bulletRB.AddForce(-bulletSpawner.transform.forward * m_Power, ForceMode.Force);
yield return new WaitForSeconds(0.25f);
}
}
}