Search Unity

how to get the user the press hold a button

Discussion in 'Android' started by path123456, May 31, 2021.

  1. path123456

    path123456

    Joined:
    Mar 20, 2021
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Shoot : MonoBehaviour
    7. {
    8.     public float damage = 10f;
    9.     public float range = 10;
    10.     public Camera cam;
    11.     public GameObject muzzleflash;
    12.     public GameObject impact;
    13.     [SerializeField] Image crosshair;
    14.     [SerializeField] Image crosshairred;
    15.     public void shootgun()
    16.     {
    17.             ShootingTime();
    18.     }
    19.    void Update()
    20.     {
    21.         RaycastHit Hit;
    22.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out Hit, 1000))
    23.         {
    24.             if (Hit.transform.tag=="Enemy")
    25.             {
    26.                 crosshair.gameObject.SetActive(false);
    27.                 crosshairred.gameObject.SetActive(true);
    28.             }
    29.             else
    30.             {
    31.                 crosshair.gameObject.SetActive(true);
    32.                 crosshairred.gameObject.SetActive(false);
    33.             }
    34.         }
    35.     }
    36.    void ShootingTime()
    37.     {
    38.         //muzzle.Play();
    39.         RaycastHit hit;
    40.         if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
    41.         {
    42.             Target target =hit.transform.GetComponent<Target>();
    43.             if (target != null)
    44.             {
    45.                 target.takedamage(damage);
    46.             }
    47.  
    48.             if (hit.rigidbody != null)
    49.             {
    50.                 hit.rigidbody.AddForce(-hit.normal * 1000f);
    51.             }
    52.             if (hit.collider.CompareTag("ENV"))
    53.             {
    54.                 GameObject impactgo = Instantiate(impact, hit.point, Quaternion.LookRotation(hit.normal));
    55.                 Destroy(impactgo, 2f);
    56.             }
    57.         }
    58.         muzzleflash.SetActive(true);
    59.         StartCoroutine(muzzlestop());
    60.     }
    61.     IEnumerator muzzlestop()
    62.     {
    63.         yield return new WaitForSeconds(0.2f);
    64.         muzzleflash.SetActive(false);
    65.     }
    66. }
    67.  
    how to get the user the press hold the shoot button