![イメージ説明]下記のようにプレイヤーが近づくと開くスライド扉のアクションに合わせてサウンドが鳴るように作成したスクリプトですが、
実行するサウンドが鳴り続けてしまいます。
開いたとき(isNear==True)に1回と閉じるとき(isNear==False)に1回鳴らしたいのですがどこを修正すればよろしいでしょうか?
C#
1コードusing System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class DoorSlide : MonoBehaviour 6{ 7 private bool isNear; 8 public float distance; // スライドする長さ 9 public AudioClip se; 10 11 12 13 Vector3 startPos; // 初期位置座標 14 Transform player; // プレイヤー座標 15 void Start() 16 { 17 player = GameObject.Find("Player").transform;// Playerという名前のオブジェクトを取得 18 startPos = transform.position; // 初期位置を代入 19 20 } 21 22 23 // Update is called once per frame 24 void Update() 25 { 26 Vector3 pos = transform.position; 27 28 if (isNear) 29 { 30 pos.z = Mathf.Lerp(pos.z, startPos.z + 2.5f, Time.deltaTime * 1.5f); 31 32 33 } 34 else 35 { 36 pos.z = Mathf.Lerp(pos.z, startPos.z, Time.deltaTime * 1.5f); 37 } 38 39 transform.position = pos; 40 } 41 42 43 44 void OnTriggerEnter(Collider other) 45 { 46 if(other.tag == "Player") 47 { 48 isNear = true; 49 GetComponent<AudioSource>().PlayOneShot(se); 50 51 } 52 } 53 private void OnTriggerExit(Collider other) 54 { 55 if(other.tag == "Player") 56 { 57 isNear = false; 58 } 59 }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/24 02:24