Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mazymol · Apr 18 at 03:44 AM · addforcebulletshootforwardbullets

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];
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mazymol · Apr 21 at 01:52 AM 0
Share

Thanks, changing the AddForce with the Velocity and the fixedDeltaTime solved the problem :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

make bullet move forward. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges