Search Unity

Question [HELP ME PLEASE] Hitmarker help pls

Discussion in 'Editor & General Support' started by SpyderManToo, Feb 5, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    I want to add a hitmarker to my aim trainer that i am currently making but the way that i have it right now is really buggy and annoying to work with. I want it to work like it enables a hitmarker icon when u hit something and it keeps it their without blinking if you are continously hitting it but when you stop shooting, the icon dissapears.

    heres my current code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MultiShotGun : MonoBehaviour
    5. {
    6.     public ScoreCounter scoreCounter;
    7.  
    8.  
    9.     public float damage = 10f;
    10.     public float range = 100f;
    11.     bool shot;
    12.  
    13.     public Camera fpsCam;
    14.     public GameObject hitmarker;
    15.  
    16.     public float _shotsFired;
    17.     public float _shotsHit;
    18.  
    19.     private float cooldown = 0.06f;
    20.  
    21.  
    22.     void Start()
    23.     {
    24.     shot = false;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.     if(cooldown > 0)
    31.     {
    32.         cooldown -= Time.deltaTime;
    33.     }
    34.  
    35.  
    36.         if(Input.GetButton("Fire1"))
    37.     {
    38.         if(cooldown <= 0)
    39.         {
    40.         cooldown = 0.06f;
    41.         SingleShotShoot();
    42.         }
    43.     }
    44.  
    45.     if(shot)
    46.     {
    47.         StartCoroutine("hitmarkerShow");
    48.     }
    49.     }
    50.  
    51.     void SingleShotShoot()
    52.     {
    53.     scoreCounter.shotsFired += 1;
    54.     RaycastHit hit;
    55.     if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    56.     {
    57.         Debug.Log("You hit" + hit.transform.name);
    58.         shot = true;
    59.         scoreCounter.shotsHit += 1;
    60.     }
    61.     }
    62.  
    63.     IEnumerator hitmarkerShow()
    64.     {
    65.     hitmarker.SetActive(true);
    66.       yield return new WaitForSeconds(0.05f);
    67.     hitmarker.SetActive(false);
    68.     shot = false;
    69.     }
    70. }
    71.  
    how do i get it to work?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Everything about the above does not seem geared to "keep it going." It's a coroutine that shows the marker and hides it.

    Instead, perhaps have a visibility timer and when you get a hit, set that to some nonzero time amount.

    Then in your Update, if it is nonzero, do 2 things:

    1 count it down by Time.deltaTime
    2 show the hitmarker as long as the timer is not zero

    That way as long as each successive hit comes in before that time expires, it should stay "hitting."
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    A timer is a pretty simple concept. It is just a float which starts at a value above 0. Every Update you subtract Time.deltaTime from it. When it gets at or below 0, the timer is triggered, so you do something. That something depends on what the timer is for.

    Simple timer example:

    Code (csharp):
    1. public float SuperAwsomeTimer = 5f;
    2.  
    3. void Update()
    4. {
    5.     if (SuperAwesomeTimer > 0f)
    6.     {
    7.         SuperAwesomeTimer -= Time.deltaTime;
    8.  
    9.         if (SuperAwesomeTimer <= 0f)
    10.         {
    11.             superAwesomeTime();
    12.         }
    13.     }
    14. }
    15.  
    16. void superAwesomeTime()
    17. {
    18.     Debug.Log("It is SUPER AWESOME TIME!!!111!!1!!!!");
    19. }
    The above code should add a log message after 5 seconds of run time.
     
  5. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i tried it but it doesn't work. could u show me what i need to do in my script.