I need help, my bullets when they start colliding with objects deviate the next time they are activated.
I do not understand why
my code is:
[Header("ObjectPool")]
[SerializeField] GameObject bulletPrefab;
GameObject parentObj;
int poolSize;
[SerializeField, HideInInspector] List<GameObject> bulletList;
private static WeaponeBSc instance;
public static WeaponeBSc Instance{ get { return instance; } }
[Header("Weapone Settings")]
[SerializeField] bool isAuto;
[SerializeField] float
bulletForce,
reloadTime,
bPS;
[SerializeField] int
maxCantMag,
maxDistance;
[SerializeField] GameObject canon;
bool
isFire,
isReload;
int
magCant,
bulletCant;
float
fireRate,
nextFire = 0f,
isReloadingTime;
GameObject playerCam, poolObj;
private void Awake(){
if(instance == null){
instance = this;
}
else{
Destroy(gameObject);
}
}
void Start(){
parentObj = GameObject.FindWithTag("PoolContainer");
poolObj = new GameObject("Pool");
poolObj.transform.parent = parentObj.transform;
poolSize = Mathf.RoundToInt(bPS * 3);
isReload = false;
isReloadingTime = reloadTime;
fireRate = 1 / bPS;
nextFire = fireRate;
bulletCant = maxCantMag * 10;
magCant = maxCantMag;
AddBulletsToPool(poolSize);
playerCam = GameObject.FindWithTag("MainCamera");
}
void Update(){
RaycastHit fireRay;
if(!Physics.Raycast(canon.transform.position, canon.transform.forward, out fireRay, .6f)){
if(isAuto){if(Input.GetMouseButton(0) && nextFire < Time.time && magCant > 0 && isReload == false){Shoot();}}
else{if(Input.GetMouseButtonDown(0) && nextFire < Time.time && magCant > 0 && isReload == false){Shoot();}}
}
Reload();
}
//Shoot
void Shoot(){
GameObject bullet = RequestBullets();
bullet.GetComponent<BulletSc>().rb.AddForce(playerCam.transform.forward * bulletForce, ForceMode.Impulse);
bullet.transform.position = canon.transform.position;
magCant -= 1;
nextFire = Time.time + fireRate;
}
//Reload
void Reload(){
if(Input.GetKeyDown(KeyCode.R) && isReload == false && isReloadingTime < Time.time){
isReload = true;
magCant = maxCantMag;
bulletCant -= maxCantMag;
isReloadingTime = Time.time + reloadTime;
}
if(isReloadingTime < Time.time)
isReload = false;
}
//Bullets
void AddBulletsToPool(int ammount){
for(int i = 0; i < ammount; i++){
GameObject bullet = Instantiate(bulletPrefab);
bullet.SetActive(false);
bulletList.Add(bullet);
bullet.transform.parent = poolObj.transform;
}
}
GameObject RequestBullets(){
for(int i = 0; i < bulletList.Count; i++){
if(!bulletList*.activeSelf){*
bulletList*.SetActive(true);*
return bulletList*;*
}
}
AddBulletsToPool(1);
bulletList[bulletList.Count - 1].SetActive(true);
return bulletList[bulletList.Count - 1];
}