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 *= Instantiate(projectileToCreate, dartPosition, dartRotation) as GameObject;*
_ projectiles*.GetComponent().speed += Random.Range(-spread, spread);_
_ projectiles.SetActiveRecursively(false);
}
}*_
* private void Activate()*
* {*
* for (int s = 0; s < spreadAmount; s++)*
* {*
* for (int i = 0; i < numberOfProjectilesToCreate; i++)*
* {*
_ if (!projectiles*.active)
{
projectiles.transform.position = new Vector3 (transform.position.x, transform.position.y + Random.Range(-spread, spread),
transform.position.z + Random.Range(-spread, spread));
projectiles.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.SetActiveRecursively(true);
projectiles.GetComponent().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?