Unity ile Yapılan Oyunda Ses Oynatma Sorunu (C#)

Egmtnc

Kilopat
Katılım
28 Mayıs 2018
Mesajlar
21
Oynumda yürürken tuşa basıldığında;

C#:
void Update()
{
    if (!GetComponent<AudioSource>().isPlaying && Input.GetKey(KeyCode.D))
    {
         GetComponent<AudioSource>().PlayOneShot(sesler[1]);
    }
}
Şu kod ile ses oynatıyorum ama yürümeyi bıraktığım anda da ses devam ediyor. Tuşu bıraktığım anda sesi nasıl durdurabilirim? şimdiden cevaplar için teşekkürler :D
 
Son düzenleyen: Moderatör:
Kod:
AudioSource audioSource;
void Start()
{

audioSource = GetComponent<AudioSource>();

}
void Update()
{
    if (Input.GetKey(KeyCode.D))
    {
         //GetComponent<AudioSource>().PlayOneShot(sesler[1]);
         audioSource.PlayOneShot(sesler[1]);
    }
    else
    {
    audioSource.mute = !audioSource.mute;
    }
    }
}
 
Kod:
AudioSource audioSource;
if (Input.GetKey(KeyCode.D))
        {
            oyuncu.transform.Translate(new Vector3(-hiz * Time.deltaTime, 0, 0));
            oyuncu.transform.localScale = new Vector3(1,1,1);
            //GetComponent<AudioSource>().PlayOneShot(sesler[1]);
            audioSource.PlayOneShot(sesler[1]);
        }
        else
        {
            audioSource.mute = !audioSource.mute;
        }
}


İlk öncelikle yorumun için teşekkürler ama işe yaramadı kullandığım kod bloğu yukarıda.
 
Deneme yapmadigim için olmadı büyük ihtimalle. Çıkan hatanın ss ini atarsanız sorunu çözebiliriz.
Kod:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OHareket : MonoBehaviour {
    public GameObject oyuncu, gm;
    public int hiz = 5, yhiz = 200;
    public bool yerde, dead, sagS, solS;
    public Rigidbody2D rb;
    public AudioClip[] sesler;
    public AudioSource audioSource;

    // Use this for initialization
    void Start () {

        rb = GetComponent<Rigidbody2D>();
        dead = gm.GetComponent<GameManager>().dead;
        audioSource = GetComponent<AudioSource>();

    }
    
    // Update is called once per frame
    void Update () {


        if (Input.GetKey(KeyCode.A))
        {
            oyuncu.transform.Translate(new Vector3(-hiz * Time.deltaTime, 0 ,0));
            oyuncu.transform.localScale = new Vector3(1,1,1);
            if (audioSource.isPlaying == false && Input.GetKey(KeyCode.A))
            {
                audioSource.PlayOneShot(sesler[1]);
                sagS = true;
            }
            else
            {
                sagS = false;
            }
            if (sagS==false)
            {
                audioSource.Stop();
                Debug.Log("A");
            }

        }

        if (Input.GetKey(KeyCode.D))
        {
            oyuncu.transform.Translate(new Vector3(hiz * Time.deltaTime, 0, 0));
            oyuncu.transform.localScale = new Vector3(-1, 1, 1);
            if (audioSource.isPlaying == false && Input.GetKey(KeyCode.D))
            {
                audioSource.PlayOneShot(sesler[1]);
                Debug.Log("1D");
            }
            else if (audioSource.isPlaying == true && Input.GetKeyUp(KeyCode.D))
            {
                audioSource.Stop();
                Debug.Log("D");
            }

        }
        if(Input.GetKeyDown(KeyCode.W) && yerde && !dead)
        {
            rb.AddForceAtPosition(new Vector2(0, yhiz), Vector2.up);
            yerde = false;
            audioSource.PlayOneShot(sesler[0]);
        }

    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Yer")
        {
            yerde = true;
        }
    }
}

kodun tamamı bu (Farklı forumlardan önerileri deniyorum bu kodda orjinal hali bu değil)
 
Uyarı! Bu konu 8 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Technopat Haberler

Geri
Yukarı