Hi all,
I have two scripts, a WeaponController (which instantiates a bullet prefab and sets the direction) and a Projectile script which fires it (by translating it forwards).
For some reason when I fire a number of bullets it seems to lag the game very heavily and I’m not quite sure what this is down to. The bullet prefab has a Projectile script attached.
If anyone can help that would be great
WeaponController
public class Weapon_Controller : MonoBehaviour
{
// The weapons that are available
public GameObject[] weaponPrefabs;
// The position to fire the weapon from - Right bullet spawner
public Transform BulletSpawnerR_obj;
private Projectile tempProjectile;
// Shoot!
public void Shoot()
{
// Instantiate a new projectile at the bullet spawner position
GameObject newProjectile = Instantiate(weaponPrefabs[currentWeapon],
BulletSpawnerR_obj.position,
BulletSpawnerR_obj.rotation) as GameObject;
// Access the Projectile script on the projectile just instantiated.
// Calling the script will fire the projectile straight away (called
// inside its Start())
tempProjectile = newProjectile.GetComponent<Projectile>();
// Make the targetPoint transform point in the bullet spawner's
// forward direction
tempProjectile.targetPoint = BulletSpawnerR_obj.forward;
//Set the weaponControllerReference to point to the current projectile
// just instantiated.
tempProjectile.weapon_ControllerReference = this;
}
}
Projectile script
public class Projectile : MonoBehaviour
{
[System.NonSerialized]
// This is the target at which the projectile will be fired
public Vector3 targetPoint = Vector3.zero;
// Use this for initialization
void Start ()
{
// Make the projectile face the target position
transform.LookAt(targetPoint);
// Destroy the projectile after a delay
Destroy(gameObject, timeToLiveInSeconds);
// Call the DoMovement() coroutine to fire the projectile forward
StartCoroutine(DoMovement());
}
IEnumerator DoMovement()
{
// While the object that this script is attached to exists..
while (true)
{
// Move projectile forward
transform.Translate(targetPoint * Time.deltaTime * 100);
}
// Waits until next fixed frame rate update function.
yield return new WaitForFixedUpdate();
}
}