Search Unity

[FIX] Collision Problem

Discussion in 'Physics' started by Black_Raptor, Jun 30, 2017.

  1. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Hey !

    I have an problem with my physics ...
    I have an bullet prefab with rigibody 3D attached on, and an sphere collider. When i shoot with my gun, he instantiate my bullet prefab and shoot him forward ( classics way ). But on my bullet script i woul whend he hit something it's break and play particle and destroy the bullet object.

    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.             if (GetComponent<Collider>().tag  == "Bullet"){
    4.  
    5.  
    6.                
    7.                 collided = true;
    8.                 GetComponent <SphereCollider> ().enabled = false;
    9.                 HitSparks.Play ();
    10.                 Destroy(gameObject, 1.5f);
    11.                
    12.                
    13.             }
    14.  
    15.  
    16.  
    17.     }
    That the part of my code, the code work, BUT collided is true directly after i shoot, so i have an invisible sphere all around my Camera Rig and bullet enter on collision with her, but i dont see where is that's invisible sphere, she not on my scene i have no collider, no object execpt my camera rig and my pistol, both don't have any collider or tag, or layer with physics. Im stuck on that since 2 day and start going crazy with that.
     
  2. Lyro_1

    Lyro_1

    Joined:
    Nov 27, 2016
    Posts:
    7
    Hello,

    Could you please show a screenshot of your Hierarchy ?
    There are two options for me :
    1. Your player prefab have some collider of any kind (spherical, cube, whatever), and this collider is so large than when the bullet is fired from your gun, it hit this collider. In this case, just resize the collider so that it will not collide anymore with the gun and the bullet.
    2. Your bullet is colliding with the gun collider :
    If so, take your gun and try to check "Is Trigger".

    According to the documentation :
    This solution could work, considering that logically you don't care about an ennemy shooting your gun. That means your gun will not take care of any collision.

    Try those two options, and comeback if none of these worked.
     
  3. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Yes it was the collider of the gun, i don't understand why because my bullet emitter was far of the gun collider, and the bullet didn't enter in collision with him. But anyway my problem is fix :D.

    Thanks you !
     
    Lyro_1 likes this.
  4. Lyro_1

    Lyro_1

    Joined:
    Nov 27, 2016
    Posts:
    7
    No problem :) Glad to know that you fixed it
    Good luck !