Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make hitmarker detect when bullets collide with something, and fade in over crosshair

Discussion in 'Scripting' started by Jivu286, Dec 3, 2023.

  1. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    I'm using physical bullets in my prototype for an arcadey air combat game. I was trying to use the bullet's own impact scripts to set the hitmarker on my ui layer to setactive when they collide with a target and didn't have any luck, so now I want to try making a script on the hitmarker gameobject itself that detects if a bullet has collided with a target. However I'm very new to coding and I'm having trouble finding resources for either method. Can anyone point me in the right direction?Z

    This is all I have so far:


    Code (CSharp):
    1. public class HitMarkerScript : MonoBehaviour
    2. {
    3.  
    4.     public GameObject bullet;
    5.     public GameObject hitMarker;
    6.  
    7.     public AudioClip hitMarkerSound;
    8.    
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         hitMarker.SetActive(false);
    13.         bullet.GetComponent<Collider>();
    14.     }
    15.  
    I did see one video saying you can use a lerp on the images transparency to fade the marker in and out, but I'd like to get the basic functionality first. Where do I go from here?
     
  2. Lenakeiz

    Lenakeiz

    Joined:
    Jun 11, 2015
    Posts:
    34
    If I understood correctly you want to detect collisions between bullets and targets, and then activate some hitmarker UI element based on the result.

    For this job I think that the
    OnCollisionEnter
    or
    OnTriggerEnter
    function are perfect.

    For example you could do something like

    Code (CSharp):
    1. // This method is called when the collider associated to this object collides with another collider
    2.     void OnCollisionEnter(Collision collision)
    3.     {
    4.         // You can check collision objects based on tags or if you want something fancier you can use a LayerMask
    5.         if (collision.other.CompareTag("Target"))
    6.         {
    7.             ActivateHitMarker();
    8.         }
    9.     }
    It s up to you to decide which object should manage the collision
     
  3. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    OnCollisionEnter on the bullet object was the original plan, I tried something like this:

    Code (CSharp):
    1. public GameObject hitMarker
    2.  
    3. void start
    4. {
    5. hitMarker.setActive(false)
    6. }
    7.  
    8. void OnCollisionEnter ()
    9. {
    10. hitMarker.setActive(true)
    11. }
    12.  
    However when I assigned my hitmarker prefab to the hitMarker gameobject, it didnt show up on my UI so I'm not sure how to call it correctly from the bullet script. That's why I thought to try a script directly on the Hitmarker UI that would get the bullet's collider and then check it for collision. But if I can do it directly from the bullet that will probably be less buggy for reuse and perform a little better
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,271
    Probably best to start with some simple tutorials on using OnCollisionEnter().

    I say this simply because this code:

    accomplishes nothing. The above code can be deleted.

    If you want to do something with the "gotten" Collider, assign it to a variable.

    Otherwise, if you want examples of colliding bullets against stuff, check out any space shooter tutorial, or you're welcome to see some of the micro-game examples in my Proximity Buttons package, such as the
    DemoSpaceShooter
    and
    DemoTwinStickShooter
    scenes... I forget how I did the collision but I think it was just coordinate checks to keep it simple and fast.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  5. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    I already have bullet collision and damage, which I used onCollisionEnter to implement. I just need a way to use that collision as a trigger to make the hitmarker ui element appear. I put bullet.GetComponent<Collider>(); because I figured I would need to do some kind of check on that collider next to see if it's collided with something damageable