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. Dismiss Notice

Resolved About how to avoid destruction. FPS and Bullets

Discussion in 'Scripting' started by VengarlofForossa, Jun 8, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Hello everyone!

    I have a question/problem with the following question.

    I have created a First Person Shooter and I have created the shooting action. It works perfectly, the bullet comes out of the gun barrel but when I shoot and walk at the same time, the bullet is destroyed because it touches the gun.

    How can I stop the bullet from being destroyed by touching the gun or the player effectively?

    In FirePoint I create the bullet.

    PlayerController.cs //Player Shoot
    Code (CSharp):
    1.      
    2. //Handle Shooting
    3.         if(Input.GetMouseButtonDown(0))
    4.         {
    5.             RaycastHit hit;
    6.             if(Physics.Raycast(camTrans.position, camTrans.forward, out hit, 50f))
    7.             {
    8.                 if (Vector3.Distance(camTrans.position, hit.point) > 2f)
    9.                 {
    10.                     firePoint.LookAt(hit.point);
    11.                 }
    12.             }
    13.             else
    14.             {
    15.                 firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
    16.             }
    17.  
    18.             Instantiate(bullet, firePoint.position, firePoint.rotation);
    19.         }
    20.  
    BulletController.cs //Bullet Life
    Code (CSharp):
    1.  
    2. private void OnTriggerEnter(Collider other)
    3.     {
    4.         if(other.gameObject.tag == "Enemy")
    5.         {
    6.             Destroy(other.gameObject);
    7.         }
    8.  
    9.         Destroy(gameObject);
    10.         Instantiate(impactEffect, transform.position + (transform.forward * (-moveSpeed * Time.deltaTime)), transform.rotation);
    11.     }
    12.  


     
    Last edited: Jun 8, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    VengarlofForossa likes this.
  3. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Thank you for your response!

    Thank you for your response

    In the end what I did was to remove the Collider from the 'gun'. I have done it this way because it is to create a simple game.

    Although I've seen your post about Layer and LayerMask to study it for other projects.

    Thank you very much!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This is actually very smart because it does keep things much simpler.

    Usually you don't want things bumping into the gun specifically.

    Glad to hear you're back on track!
     
    VengarlofForossa likes this.