Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bullet Collision not consistent?

Discussion in 'Scripting' started by Rob21894, Jul 24, 2017.

  1. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    My bullets aren't detecting collision consistently, infact its every other bullet registers as a collision.

    I have a rigid body on the bullet and have tried applying a rigidbody to the object but it gives the same results with or without.


    The code below shows the collisions working, I have tried a linecast and a spherecast but both don't seem it improve anything. However I know the issue is due to the bullet travelling too fast but what bullet travels slow? :p

    Is there any workarounds for this? I'm having no luck browing the forums

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletControl : MonoBehaviour {
    6.  
    7.     [Range(0, 50)]
    8.     public float bulletSpeed;
    9.     public float deactivateTimer;
    10.     float tempDeactivateTimer;
    11.     bool bulletHasHit = false; // bullet has hit something
    12.     void Start()
    13.     {
    14.         tempDeactivateTimer = deactivateTimer;
    15.     }
    16.     Vector3 prev = Vector3.zero;
    17.     // Update is called once per frame
    18.     void FixedUpdate()
    19.     {
    20.  
    21.         if (!bulletHasHit)
    22.         {
    23.             BulletHitDetection();
    24.         }
    25.       //  transform.Translate(transform.forward * bulletSpeed);
    26.  
    27.     }
    28.  
    29.     void LookForCollision()
    30.     {
    31.         Collider[] hitDetection = Physics.OverlapSphere(gameObject.transform.position, .5f, 1 << 9, QueryTriggerInteraction.Collide);
    32.  
    33.  
    34.  
    35.         if (hitDetection.Length > 0)
    36.         {
    37.             float closestObjectDist = 10000f;
    38.             GameObject closestObj = hitDetection[0].gameObject;
    39.  
    40.             for (int i = 0; i < hitDetection.Length; i++)
    41.             {
    42.                 if (Vector3.Distance(gameObject.transform.position, hitDetection[i].transform.position) < closestObjectDist)
    43.                 {
    44.                     Debug.Log(hitDetection[i].name);
    45.                     closestObjectDist = Vector3.Distance(gameObject.transform.position, hitDetection[i].gameObject.transform.position);
    46.                     closestObj = hitDetection[i].gameObject;
    47.                 }
    48.             }
    49.             BulletHitObject(closestObj);
    50.         }
    51.     }
    52.     public void BulletHitDetection()
    53.     {
    54.         RaycastHit hit;
    55.         if (!bulletHasHit)
    56.         {
    57.             LookForCollision();
    58.             if (prev != Vector3.zero)
    59.             {
    60.                 if (Physics.Linecast(prev, transform.position, out hit, 1 << 9, QueryTriggerInteraction.Collide))
    61.                 {
    62.                     BulletHitObject(hit.collider.gameObject);
    63.                 }
    64.             }
    65.             else
    66.             {
    67.                 prev = transform.position;
    68.             }
    69.  
    70.         }
    71.  
    72.  
    73.  
    74.     }
    75.  
    76.     public void BulletHitObject(GameObject hitObject)
    77.     {
    78.         bulletHasHit = true;
    79.        // Debug.Log(bulletHasHit);
    80.         if (hitObject.tag == "Wall")
    81.         {
    82.             Debug.Log("Wall");
    83.         }
    84.  
    85.  
    86.         this.gameObject.SetActive(false);
    87.     }
    88.  
    89.  
    90.     void OnDrawGizmos()
    91.     {
    92.         Gizmos.color = Color.yellow;
    93.         Gizmos.DrawSphere(transform.position, .5f);
    94.     }
    95. }
    96.  
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Bump, been trying with a simple raycast too and still ignores collision 90% of the time.. anyone know why?

    Code (CSharp):
    1.             if (Physics.Raycast(transform.position, -transform.up, out hit, .5f, 1 << 9, QueryTriggerInteraction.Collide))
    2.             {
    3.                BulletHitObject(hit.collider.gameObject);
    4.             }
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    raycast and linecast are the same thing.

    try removing the layermask and querytriggerinteraction enum from teh call.

    Side note.. I would not do a overlapshere call. If you were going to do that, do a SphereCast instead of a ray/linecast.

    In your BulletHitObject method, you are disabling the bullet. I would destroy it to clean up clutter in your scene. (unless you have a specific purpose for that.)
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309

    Just fixed the problem from casting a linecastfrom the bullet start pos to its current pos until it hits an object,

    The bullet deactives due to ObjectPooling so it reuses the same bullets so i don't have to instantiate every time :)