サンタゲーム用スクリプト
サンタゲームのメインスクリプト.単眼VRで照準に入ったら色を変えたり,スコアを表示したり,残時間を表示するなど.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class scope: MonoBehaviour { [Header("クロスヘア(照準)の画像をつけたUIを入れる")] public Image aimPointImage; [Header("スライダー(補足時間目安)のUIを入れる")] public Slider myslider;//UIのスライダー float slideValue = 0f;//スライダーの値用の変数 bool soundBool = false;//音を連続再生されるのを防止するBool [Header("何点ゲットしたかを表示する画像用のUI")] public Image pointImage;//何点ゲットしたかを表示する画像用のUI [Header("上のImageが入っているGameObject")] public GameObject imageObj;//上のImageが入っているGameObject [Header("10点用画像")] public Sprite point10;//Imageに割り当てる画像(スプライト10点用以下同じ) [Header("20点用画像")] public Sprite point20; [Header("50点用画像")] public Sprite point50; [Header("クロスヘアにロックした時の音")] public AudioClip scopesound;//クロスヘアにロックした時の音 [Header("プレゼントをゲットした音")] public AudioClip getsound;//プレゼントをゲットした音 AudioSource audio;//オーディオソース用 AudioSource audio2; [Header("ポイント表示用UI")] public Text pointTxt;//文字変数ポイント用 [Header("時間表示用UI")] public Text timeText;//時間表示用文字 int mypoint;//ポイント用Int [Header("初期残時間")] public int mytime;//時間用 private void Start() { AudioSource[] audioSources = gameObject.GetComponents<AudioSource>();//Maincameraにアサインされている複数のAudioSourceを取得し配列に入れる audio = audioSources[0];//一つ目のオーディオソースの名前をaudioに audio2 = audioSources[1]; imageObj.SetActive(false);//ポイント取得画像パネルを隠す mypoint = 0; pointTxt.text = mypoint.ToString();//UIの文字を初期化する timeText.text = mytime.ToString();//上と同じ Invoke("startTimer", 3f);//3秒後にタイマースタート } void FixedUpdate() { // Rayを飛ばす Ray ray = new Ray(transform.position, transform.forward); // outパラメータ用に、Rayのヒット情報を取得するための変数を用意 RaycastHit hit; // Rayのhit情報を取得する if (Physics.SphereCast(ray, 0.5f, out hit, 80.0f)){//Rayで球を飛ばす // Rayがhitしたオブジェクトのタグ名を取得 string hitTag = hit.collider.tag; // タグの名前がpresentだったら、照準の色が変わる if ((hitTag.Equals("present"))){ //照準を赤に変える aimPointImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f); slideValue += 1.5f;//スライドバー用の値をアップさせる myslider.value = slideValue;//スライドバーの値をセットする if(!soundBool){//連打再生されないようにBoolで飛ばす soundBool = true; audio.PlayOneShot(scopesound, 0.2f);//スコープ用の音を再生する } //99越えたら消す if(slideValue > 99){ pointget(hit.collider.gameObject); Destroy(hit.collider.gameObject); soundBool = false;//再び再生できるようにする } }else{ // present以外では水色に aimPointImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f); slideValue = 0f;//スライド値を0にする myslider.value = slideValue;//その値をスライドにセットする audio.Stop();//音再生停止 soundBool = false; } }else{ // Rayがヒットしていない場合は水色に aimPointImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f); slideValue = 0f; myslider.value = slideValue; audio.Stop(); soundBool = false; } } void pointget(GameObject go){ //音再生 audio2.PlayOneShot(getsound); //ポイント表示 //InstatiateしたPrefab名で足す点数を変えている //と同時にポイント用画像を選定してセットする if (go.name.Contains("PresentYel")) { pointImage.sprite = point10; mypoint += 10; } else if (go.name.Contains("PresentBlue")) { pointImage.sprite = point20; mypoint += 20; } else if (go.name.Contains("PresentRed")) { pointImage.sprite = point50; mypoint += 50; } PlayerPrefs.SetInt("score", mypoint);//End画面用にPlayerPrefsに得点をセットする imageObj.SetActive(true);//得点画面を出す Invoke("hidePoint", 1f);//1秒後にポイント画面を隠す pointTxt.text = mypoint.ToString();//ポイントを文字列に変更して表示 } void hidePoint(){ imageObj.SetActive(false);//ポイント画面を隠す } void startTimer(){ StartCoroutine ("myTimer"); //コルーチンをスタートする } IEnumerator myTimer() { while (true) { mytime--;//初期設定の120秒を減らす if(mytime < 10){//10秒未満になったら残り時間を変える timeText.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);//文字を赤色に }else{ timeText.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);//文字を白色に } timeText.text = mytime.ToString(); yield return new WaitForSeconds(1.0f);//1秒間待つ //終了処理 if(mytime < 1){ Invoke("showScore", 2f); yield break; //終了処理へ } } } //やめるボタン用 public void goHomeBtn(){ SceneManager.LoadScene("op"); } //タイムアップ処理 void showScore(){ //スコア表示画面へ SceneManager.LoadScene("end"); } }
スクリプトの使い方です.説明文を入れていますので参考に.
