- Home /
bullets don't go forward
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[i].activeSelf){
bulletList[i].SetActive(true);
return bulletList[i];
}
}
AddBulletsToPool(1);
bulletList[bulletList.Count - 1].SetActive(true);
return bulletList[bulletList.Count - 1];
}
Answer by Elias_g · Apr 18 at 11:29 AM
In your Shoot function you add force to the bullet's RigidBody. RigidBody does not automatically reset its velocity after being disabled, therefor it gets all messed up when you fire the same bullet multiple times, velocity stacks. You can try setting the velocity directly in your weapon script's Shoot function with the built-in .velocity variable or reset velocity in the bullet script.
For example:
//Shoot
void Shoot(){
GameObject bullet = RequestBullets();
bullet.GetComponent<BulletSc>().rb.velocity = playerCam.transform.forward * bulletForce * Time.fixedDeltaTime;
bullet.transform.position = canon.transform.position;
magCant -= 1;
nextFire = Time.time + fireRate;
The Time.fixedDeltaTime multiplication is done behind the scenes if you use AddForce function, so I included it just in case.
My other guess is that some values you may be using in your bullet script, like collided flag or something like that, should also be reset to the initial state after bullet is disabled.
Hope that helps.
Thanks, changing the AddForce with the Velocity and the fixedDeltaTime solved the problem :D
Your answer
Follow this Question
Related Questions
make bullet move forward. 2 Answers
Add force to Instantiated object. 4 Answers
Bullets shooting the wrong way with AddForce 1 Answer
Issues with bullet not subtracting properly 1 Answer
The code is giving me errors 1 Answer