Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

About the FPS bullet launch position

Discussion in 'Editor & General Support' started by kumao23, Jun 27, 2021.

  1. kumao23

    kumao23

    Joined:
    Jan 19, 2015
    Posts:
    11
    When firing bullets by left-clicking, the firing position changes to the right, left, or near the middle of the screen.
    I've made the main camera a child of the empty object and fire from there.

    Video of a run with a slower bullet exit speed.


    Source Code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shoot : MonoBehaviour
    6. {
    7. public GameObject Bullet;
    8. Rigidbody rb_bullet;
    9. public float thrust;
    10.  
    11. // Start is called before the first frame update
    12. void Start()
    13. {
    14.  
    15. }
    16.  
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. if (Input.GetMouseButtonDown(0))
    21. {
    22. rb_bullet = Instantiate(Bullet, transform.position, transform.rotation).GetComponent<Rigidbody>();
    23. rb_bullet.AddForce(transform.forward * thrust, ForceMode.Impulse);
    24. }
    25. }
    26. }
    Video with continuous firing rate using a different script.


    Source Code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletShoot : MonoBehaviour
    6. {
    7.     public GameObject bulletPrefab;
    8.     public float shotSpeed;
    9.     private int shotCount;
    10.     public int MaxShotcount;
    11.     private float shotInterval;
    12.     public AudioClip ShootSE;
    13.     public AudioClip ReloadSE;
    14.  
    15.     private float timer;
    16.  
    17.    
    18.     void Start()
    19.     {
    20.         shotCount = 0;
    21.     }
    22.  
    23.    
    24.     void Update()
    25.     {
    26.         timer += Time.deltaTime;
    27.  
    28.  
    29.         if (Input.GetKey(KeyCode.Mouse0))
    30.         {
    31.             shotInterval += 1;
    32.  
    33.             if (shotInterval % 5 == 0 && shotCount > 0)
    34.             {
    35.                 shotCount -= 1;
    36.  
    37.                 GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
    38.                 Rigidbody bulletRb = bullet.GetComponent<Rigidbody>();
    39.                 bulletRb.AddForce(transform.forward * shotSpeed);
    40.                 AudioSource.PlayClipAtPoint(ShootSE, transform.position);
    41.             }
    42.  
    43.         }
    44.         else if (Input.GetMouseButton(1) && timer > 1.0f)
    45.         {
    46.             shotCount = MaxShotcount;
    47.             AudioSource.PlayClipAtPoint(ReloadSE, transform.position);
    48.             timer = 0.0f;
    49.         }
    50.     }
    51.     void OnTriggerEnter(Collider other)
    52.     {
    53.         if (other.gameObject.tag == "Bullet")
    54.         {
    55.             shotCount += MaxShotcount;
    56.         }
    57.     }
    58. }
    Inspector image


    Even if I turned off the Sphere Collider in the inspector, the phenomenon of the sphere being split into left and right besides the center did not change.
    Even if I changed the script, the behavior from the launching sphere was the same, which made it even more difficult for a beginner to figure out what to fix.
    If you can help me, please let me know.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    If you think the problem is the bullet collider interacting with your player, use separate layers for each class of object and go into physics settings to change what layers collide with each other. It's a VERY easy fast test, then if it fixes things you're done, otherwise you can investigate other avenues.

    https://docs.unity3d.com/2019.2/Documentation/Manual/class-PhysicsManager.html
     
  3. kumao23

    kumao23

    Joined:
    Jan 19, 2015
    Posts:
    11
    Dear Kurt-Dekker.
    Thank you very much!
    I immediately tried assigning the firing spheres and bullets to different layers. The behavior was the same for both, even with the intersection check on and off.
    I'm not sure if I'm doing it wrong...
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Keep stripping it down until all that is left is what causes the problem!
     
  5. kumao23

    kumao23

    Joined:
    Jan 19, 2015
    Posts:
    11
    Dear Kurt-Dekker.
    Thanks again.

    It was definitely getting messy, so I reassembled only the new firing part to determine the cause again.
    And now I know one more thing!
    I'll add a video for that.



    The script and sounds used are inherited from the previous one, so I won't describe them.
    So, the first time I run it, the bullets still shake off-center. However, once you turn off the firing script and turn it back on, it will fire normally only in the center.
    I'm still trying to figure out what the heck is going on here.
    Is there anything else I can try?
     
  6. JRRReynolds

    JRRReynolds

    Joined:
    Oct 29, 2014
    Posts:
    192
    Does the issue still happen if you instantiate the bullet and have it inherit the camera's world (not local) rotation and then add force to the bullet's transform forward? Also since you are using physics bullets, you may want to poll for the input in update but do the instantiation/forces in the next fixed update frame.
     
  7. kumao23

    kumao23

    Joined:
    Jan 19, 2015
    Posts:
    11
    Thanks JRRReynolds!
    It's quite a challenge for me as a beginner in unity. Hahahaha.... It's sad...
    I've made a zip file of the project folder of the minimal configuration.
    As a last resort, I'll try to see if I can kill the bug by controlling the script on/off for the time being. However, it's not a fundamental solution or study, so I'd like you to take a look at the project and let me know.
    Thank you very much for your time.

    https://firestorage.jp/download/f79b3e40a1dac1733175bb28866532d9c7b8519c
     
  8. kumao23

    kumao23

    Joined:
    Jan 19, 2015
    Posts:
    11
    It's solved!
    I was able to check IsKinematic and it works fine. I can finally move on. Thank you all very much for your help.