- Home /
How can I fire multiple projectiles at once?
I'm making a first person shooter, and I want to make it so that the shotgun fires 2 bullets/darts at once, the problem is, the darts are always facing the same direction, even if I shoot in another, and sometimes, it just freezes in mid-air, sometimes, it stutters in mid-air.
Here's my code:
using UnityEngine;
using System.Collections;
public class ObjectPool : MonoBehaviour {
GameObject[] projectiles;
public GameObject projectileToCreate;
public int numberOfProjectilesToCreate;
public RayShoot gunScript;
public float spread;
public byte spreadAmount;
public enum shootType
{
normal = 0,
spread = 1,
multiSource = 2
}
public shootType typeOfGun;
public byte lifeSpan;
// Use this for initialization
void Start ()
{
projectiles = new GameObject[numberOfProjectilesToCreate];
InstantiateProjectiles();
}
// Update is called once per frame
void Update ()
{
if (gunScript.shooting)
{
Activate();
}
}
private void InstantiateProjectiles()
{
for (int i = 0; i < numberOfProjectilesToCreate; i++)
{
Vector3 dartPosition = new Vector3 (transform.position.x, transform.position.y + Random.Range(-spread, spread),
transform.position.z + Random.Range(-spread, spread));
Quaternion dartRotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
projectiles[i] = Instantiate(projectileToCreate, dartPosition, dartRotation) as GameObject;
projectiles[i].GetComponent<ProjectileScript>().speed += Random.Range(-spread, spread);
projectiles[i].SetActiveRecursively(false);
}
}
private void Activate()
{
for (int s = 0; s < spreadAmount; s++)
{
for (int i = 0; i < numberOfProjectilesToCreate; i++)
{
if (!projectiles[i].active)
{
projectiles[i].transform.position = new Vector3 (transform.position.x, transform.position.y + Random.Range(-spread, spread),
transform.position.z + Random.Range(-spread, spread));
projectiles[i].transform.rotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
projectiles[i].SetActiveRecursively(true);
projectiles[i].GetComponent<ProjectileScript>().Activate();
return;
}
}
}
}
}
}
}
-----------------------------------------------------------
using UnityEngine;
using System.Collections;
public class ProjectileScript : MonoBehaviour {
public float lifeSpan;
public float speed;
private float timeToDie;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
CountDown();
//AutoMove();
}
public void Activate()
{
timeToDie = Time.time + lifeSpan;
transform.position = Vector3.zero;
Vector3 velocity = speed * Time.deltaTime * transform.forward;
transform.Translate(velocity);
}
private void Deactivate()
{
this.gameObject.SetActive(false);
}
private void CountDown()
{
if (timeToDie < Time.time)
{
Deactivate();
}
}
}
How do I fix this?
This is a script that ios attached to your weapon?
What type is the "projectile" variable? a prefab?
Oh, I'm sorry, projectile is a rigidbody.
This script is kind of attached to the weapon, not attached to the weapon itself, but attached to an empty that's a child of the weapon.
so this "projectile" rigidbody is attached to a some 'master' bullet that you are making copies from?
Also, have you tested to make sure that your "dartRotation" quaternion is the proper value?
Answer by nsxdavid · Feb 24, 2013 at 08:27 PM
transform.rotation
is Quaternion. You are seeding your dartRotation Quaternion from Euler angles, however you are randomizing off of components of the Quaternion.
So you probably intended:
Quaternion dartRotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
It worked perfectly! Thank you!
Do you have any idea how I can fix the freezing/stuttering in mid-air problem?
Not enough code to really look into that.
I will note that your Destroy() call should just have simple seconds as the final argument. If you want the projectile to be deleted in 5 seconds, the last argument should be 5. Frame rate has nothing to do with it.
Well, my guns fire fast, and so the game becomes filled with darts, and slows the game right down, the destroy framerate command help sort that, it avoids more bullets co$$anonymous$$g out to slow it down more, and kills the bullets already out quicker, improving the framerate, and it works.
Better to keep a count of how many projectiles are in the scene and keep it capped. Either don't allow any more when you hit the limit, or destroy the oldest, etc.
Also consider not using physics if you don't really need it. This might be overkill for a $$anonymous$$igore like game.
(Regarding updated code)
You create a ObjectPool class, which is a good start. However you made it specific to Projectiles. A better design is for it to be generic so you can pool all sorts of things.
Be that as it may, you are creating all the projectiles on startup. They are being positioned and oriented all at once based on the transform of the object that you have the ObjectPool behavior on. This is why they are all on the same position and firing the same direction.
You need to ins$$anonymous$$d initialize the position and rotation of the projectile at the time of activation, and in relation to the gun/player/whatever not in relation to the ObjectPool. Unless, of course, the ObjectPool behavior is on the gun/player. Then it's okay to use the transform, but you still need to do position/orientation at the time the player shoots not at startup.