First of all:
The projectile prefab is on a layer and the physics have been set to ignore collision with the player and other projectiles in the project settings. The projectile has a rigidbody with all the rotation axes disabled. It also has the axis that I’m not using in my 2D game disabled.
When I fire the projectiles from my player, they fire the way I want them too, but as soon as they start to collide with another object with a rigidbody, some of the change direction. Then, after a while of firing, all the projectiles start doing the same thing and it stays that way until I end the play session.
I’d also like to add that the projectiles are not randomly changing direction, it is predictable and has the same pattern of movement throughout the firing of the cloned projectiles.
public class FireProjectile : MonoBehaviour
{
public GameObject projectilePrefab;
public GameObject[] clone;
private Transform thisTransform;
private int ammo;
private int lastBullet;
private int curBullet;
private bool locked;
private float rof;
void Awake()
{
ammo = 50;
lastBullet = 0;
rof = 0.05f;
clone = new GameObject[ammo];
thisTransform = this.transform;
InstantiateProjectiles();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Space))
{
if(IsInputLocked()){}
else
{
ActivateProjectiles();
Lock();
}
}
}
private void InstantiateProjectiles()
{
for(int i = 0; i < clone.Length; i++)
{
clone *= (Instantiate(projectilePrefab, thisTransform.position, thisTransform.rotation) as GameObject);*
_ clone*.SetActive(false);_
_ }*_
* }*
* private void ActivateProjectiles()*
* {*
* curBullet = GetNextBullet();*
* if(clone[curBullet].activeSelf == false)*
* {*
* clone[curBullet].SetActive(true);*
* clone[curBullet].GetComponent().Activate();*
* clone[curBullet].transform.position = thisTransform.position;*
* } *
* }*
* private void Lock()*
* {*
* locked = true;*
* Invoke(“Unlock”, rof);*
* }*
* private void Unlock()*
* {*
* locked = false;*
* }*
* private bool IsInputLocked()*
* {*
* return locked;*
* }*
* private int GetNextBullet()*
* {*
lastBullet += 1;
if(lastBullet >= ammo-1)
* {*
lastBullet = 0;//reset the loop
}
* Debug.Log("LastBullet = " + lastBullet);*
return lastBullet;
}
}
public class Projectile : MonoBehaviour
{
* private float projectileSpeed;*
* public PlayerBounds bounds;*
* private float time;*
* private float lifeTime;*
* private Transform thisTransform;*
* void Awake()*
* {*
* lifeTime = 2.0f;*
* projectileSpeed = 5.0f;*
* thisTransform = transform;*
* }*
* // Update is called once per frame*
* void Update ()*
* {*
* LifeSpan();*
* MoveProjectile();*
* }*
* private void MoveProjectile()*
* { *
_ float movement = projectileSpeed * Time.deltaTime;
thisTransform.Translate(Vector3.right * movement);_
* }*
* public void Activate()*
* {*
* time = Time.time + lifeTime;*
* }*
* public void Deactivate()*
* {*
* gameObject.SetActive(false);*
* }*
* private void LifeSpan()*
* {*
* if(time <= Time.time)*
* {*
* Deactivate();*
* }*
* }*
}