スイッチを押すと1度だけ音をだしてそれ以降は音を出さないようにしたいのですがこのスクリプトだと踏むたびに音がなってしまいます。PlayOneShotも試しましたが同じでした。
原因としてはTriggerEnter2Dの中に書いててオブジェクトに入るたびに反応してるからなんだと思うのですがどういう感じに書き直したらいいか分かんないです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class switchRs : MonoBehaviour { public GameObject ob; public AudioClip SwitchON; public AudioClip SE; // Start is called before the first frame update void Start() { ob.SetActive(false); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Player") { AudioSource.PlayClipAtPoint(SwitchON, transform.position); AudioSource.PlayClipAtPoint(SE, transform.position); ob.SetActive(true); } } // Update is called once per frame void Update() { } }
質問者さん、解決したならベストアンサー決めましょう。
今のままだと未解決扱いです。
回答1件
あなたの回答
tips
プレビュー