void Shoot() { Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f)); ray.origin = cam.transform.position; if(Physics.Raycast(ray, out RaycastHit hit)) { hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage); }
// проигрываем звук выстрела if (shootingSound != null && audioSource != null) { audioSource.PlayOneShot(shootingSound); } } }
не работает произведение звука при стрельбе There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
using System.Collections.Generic;
using UnityEngine;
public class SingleShotGun : Gun
{
[SerializeField] Camera cam;
[SerializeField] AudioClip shootingSound; // добавляем поле для аудиофайла
private AudioSource audioSource; // создаем переменную для AudioSource
void Start()
{
audioSource = GetComponent<AudioSource>(); // получаем ссылку на AudioSource
}
public override void Use()
{
Shoot();
}
void Shoot()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
ray.origin = cam.transform.position;
if(Physics.Raycast(ray, out RaycastHit hit))
{
hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
}
// проигрываем звук выстрела
if (shootingSound != null && audioSource != null)
{
audioSource.PlayOneShot(shootingSound);
}
}
}
не работает произведение звука при стрельбе
There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.