Search Unity

Need help finding a way to get a consistent bullet collision

Discussion in 'Scripting' started by Robster95, Jan 23, 2021.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I have a prefab of a bullet being shot from a gun attached to the player.

    The bullet is flying fast enough for there to be a few problems with the oncollisionEnter detection.

    I made a debug.Log(transform.postion) in the oncollisionenter and the bullet is reading a different vector each time it runs the debug

    I've tried making a raycast that'll check in front of the bullet and find a spot to collide with, but I wasn't able to get that to work either.

    If anyone knows how to check for a consistent collision spot on a fast moving bullet can you please help me?
    Thank you.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Don't use a monobehavior for each bullet, it's not gonna scale well.
    What was your problem with the raycast? instead of saying "it didn't work" say what didn't work, show your code.
    I always do bullets with raycasts myself so i know it works.
     
    Robster95 and Kurt-Dekker like this.
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Sorry I was at work all day today. what should I do instead of having it call from Monobehaviour?

    For the raycast not working, I have a raycast on the bullet that checks to see if there is a wall in front of the bullet to check if there will be a collision because the bullet is moving so fast sometime the bullet skips through the collision and collides at a different point on the collider it is hitting.

    For the code I have attached to the bullet, this it it. I changed a few things after I posted this yesterday and I'm not to sure where I left off so if there are some pieces that dont make sense, that's probably where I left off.

    Code (csharp):
    1.  
    2. [SerializeField]
    3.     private float bulletSpeed;
    4.  
    5.     private int bulletDamage;
    6.  
    7.     private bool destroyOnNextFrame;
    8.  
    9.     Vector3 distance;
    10.  
    11.     public int damage
    12.     {
    13.         get => bulletDamage;
    14.         set => bulletDamage = value;
    15.     }
    16.  
    17.     private void Start()
    18.     {
    19.         Destroy(gameObject, 2);
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (destroyOnNextFrame)
    25.         {
    26.             Destroy(gameObject);
    27.             return;
    28.         }
    29.  
    30.         distance = transform.forward * bulletSpeed * Time.deltaTime;
    31.         RaycastHit hit;
    32.  
    33.  
    34.  
    35.         if (Physics.Raycast(transform.position, transform.forward, out hit, distance.z))
    36.         {
    37.             Debug.Log("Hitting S***z");
    38.  
    39.             transform.position = hit.point;
    40.  
    41.             ApplyDamage(hit.transform);
    42.  
    43.             destroyOnNextFrame = true;
    44.         }
    45.  
    46.         else
    47.         {
    48.             transform.position += distance;
    49.         }
    50.     }
    51.  
    52.     private void ApplyDamage(Transform hitObject)
    53.     {
    54.         HealthBehaviour health;
    55.  
    56.         health = hitObject.transform.GetComponent<HealthBehaviour>();
    57.  
    58.         health?.ApplyHealthChange(bulletDamage);
    59.     }
    60.  
    61.    
    62.  
    63.     //private void OnCollisionEnter(Collision collision)
    64.     //{
    65.     //    Debug.Log("Hit with collision");
    66.        
    67.     //    ApplyDamage(collision.transform);
    68.  
    69.     //    Destroy(gameObject);
    70.     //}
    71.  
    72.     //private void OnDestroy()
    73.     //{
    74.     //    Debug.Log(transform.position);
    75.     //}
    76. }
    77.  
    I want the raycast to check in front of the bullet and look for a collision so the bullet will always collide and be destroyed at the same spot (if the player and bullet angle doesn't change)