Search Unity

Question Play sound on use()

Discussion in 'Audio & Video' started by jcjms, Mar 23, 2021.

  1. jcjms

    jcjms

    Joined:
    Jul 24, 2018
    Posts:
    14
    Hi,
    i would like to play an audiclip (gun_shot) every time i use my gun in a game that is the part of my script it should be in:

    Code (CSharp):
    1. public override void Use()
    2.     {
    3.         Shoot();
    4.         Shoot_Sound();
    5.     }
    6.  
    7.     void Shoot()
    8.     {
    9.         Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
    10.         ray.origin = cam.transform.position;
    11.         if(Physics.Raycast(ray, out RaycastHit hit))
    12.         {
    13.             hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
    14.             PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal);
    15.         }
    16.     }
    17.  
    18.     void Shoot_Sound()
    19.     {
    20.         //here i want to play my sound (gun_shot)
    21.     }
    I hope you can help me. Thank you.