Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Bug Problems with Physics Bullet Collision

Discussion in 'Physics' started by Jivu286, Nov 29, 2023.

  1. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Hello,

    Currently I've given an aircraft guns by spawning physical bullets on mouse click. I have a simple health script and I've successfully made the bullet object remove health when it collides with any object that has "UnitHealth" script attached. I have confirmed this works by successfully destroying a truck with the script attached.

    There are a few problems, though. First, the bullet seems to not be colliding with the truck if it's farther than maybe 5 grid squares away. This made me notice that the bullet is also not self-destructing as it should when it collides with the stretched cube I'm using as terrain, which does have a collider. Finally, I am also getting an error message every time the bullet collides with something that does not have the "UnitHealth" script as a component (ie when it collides with the floor)
    This is my bullet script:

    Code (CSharp):
    1. public class BulletOneScript : MonoBehaviour
    2. {
    3.     public Rigidbody rigid;
    4.     public float speed;
    5.     public GameObject CannonOneMuzzleFlash;
    6.     public GameObject playerCollision;
    7.     public GameObject bulletCollision;
    8.     public float damageDealt;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rigid.AddRelativeForce(Vector3.forward * speed);
    13.     }
    14.     public void OnTriggerEnter(Collider collider)
    15.     {
    16.         bulletCollision.GetComponent<Collider>());
    17.         //damage code
    18.         collider.gameObject.GetComponent<UnitHealth> ().Damage(damageDealt);
    19.         Destroy(this.gameObject);
    20.     }
    21. }
    Sort of related but not vital for this question - I had to put the spawn points for the bullets pretty far ahead of the visual cannon barrels because when the plane is moving, if the bullets don't spawn a ways forward then they spawn inside the plane's collider and disappear. So the bullets despawning on collision does work in that case, I don't know why they don't otherwise.
    I think I've heard that physical bullets are a pain and its better to raycast, but I want the projectiles to have travel speed and drag and gravity. Are my issues with collision just down to unity itself or is there something I can do?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    979
    Triggers can only use discrete collision detection which often fails to detect collisions of fast moving objects.

    Instead use OnCollisionEnter and set the collision detection mode on the rigidbody to Continuous.

    Code (CSharp):
    1.     public void OnCollisionEnter(Collision c)
    2.     {
    3.         c.gameObject.GetComponent<UnitHealth>().Damage(damageDealt);
    4.         Destroy(this.gameObject);
    5.     }
     
  3. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Thank you!

    I asked elsewhere and was also shown how to do an if statement to TryGetComponent so that the getcomponent only runs if the object collided with does have the UnitHealth script, but VS is giving me grief about using that statement in OnCollisionEnter.

    When I try:
    Code (CSharp):
    1. public void OnCollisionEnter(Collider collider)
    2. {
    3.  
    4.     if (collider.TryGetComponent(out UnitHealth health))
    5.     {
    6.         health.Damage(damageDealt);
    7.         Destroy(gameObject);
    8.     }
    9.  
    10.     Destroy(this.gameObject);
    11. }
    it tells me "The Unity message 'OnCollisionEnter' has an incorrect signature."

    When I try
    Code (CSharp):
    1. public void OnCollisionEnter(Collision collision)
    2. {
    3.  
    4.     if (collision.TryGetComponent(out UnitHealth health))
    5.     {
    6.         health.Damage(damageDealt);
    7.         Destroy(gameObject);
    8.     }
    it tells me
    "Collision' does not contain a definition for 'TryGetComponent' and no accessible extension method 'TryGetComponent' accepting a first argument of type 'Collision' could be found (are you missing a using directive or an assembly reference?)"

    Any ideas for how to keep this solution to the script calling getcomponent on everything it touches and giving an error if the target isnt present?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,795
    Collission
    has a
    collider
    property. So just use
    collision.collider.TryGetComponent
    .

    You can always check these things in the documentation: https://docs.unity3d.com/ScriptReference/Collision.html

    You can also check the available options in your IDE's auto-complete to see what might be relevant.
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    979
    Oops! Sorry, I should've added the component check. Although it's common to test the colliding object's tag to see if it's of a certain type before accessing a component and so checking for a component's presence isn't always necessary.
     
  6. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Alright, looks like I've got everything set regarding this particular piece of code. Setting both the bullet and target rigidbody to Continuous detection and that small adjustment to TryGetComponent did work. However I had to disable "is trigger" on the bullet.

    Hit detection does work at longer ranges now, but for some reason its way spottier and less consistent than at close ranges. I suppose it's time to add a debug line to the onCollisionEnter, this may be a separate issue/topic though. Thank you guys for the help
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    979
    Yep, disabling the trigger is the way to go.

    If your target objects are moving then setting the collision detection mode to 'Continous Dynamic' on your bullets will be even more reliable. And if your objects are quite small then increasing their collider size will also help.
     
    Last edited: Dec 2, 2023
  8. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Final update: Setting the rigidbodies for both the bullet and the target to "interpolate" fixed the buggy detection at range. All set for this issue, thanks again