前提・実現したいこと
現在、カーリングゲームを作成しており、ストーンを一投投げるごとに距離を図るスクリプトを作成しています。
しかし、一投目だけ条件分岐の判定がされず、その原因がわかりません。
発生している問題・エラーメッセージ
二投目からはしっかりと判定してくれますが、一投目だけ判定しません。
該当のソースコード
手前の赤ストーンにStone.cs
奥の白い円にNearestScript.csが入ってます。
白い円からStoneまでの距離をとっています。
Stone.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Stone : MonoBehaviour { 6 public static Rigidbody rb; 7 public GameObject isi; 8 public static bool Ika = false; 9 // Use this for initialization 10 void Start () { 11 rb = GetComponent<Rigidbody>(); 12 } 13 14 // Update is called once per frame 15 void Update () { 16 if (Input.GetKey("up")) 17 { 18 Ika = true; 19 gameObject.GetComponent<Rigidbody>().velocity = transform.forward * 300f; 20 21 } 22 if (Input.GetKey("right")) 23 { 24 rb.AddForce(Vector3.right * 100f); 25 } 26 if (Input.GetKey("left")) 27 { 28 rb.AddForce(Vector3.left * 100f); 29 } 30 if (rb.IsSleeping() && Ika) 31 { 32 Vector3 pos = transform.position; 33 pos.x = 0; 34 pos.y = 0; 35 pos.z = -700; 36 Instantiate(isi, pos, transform.rotation); 37 Ika = false; 38 gameObject.GetComponent<Stone>().enabled = false; 39 40 } 41 } 42 43} 44
NearestScript.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class NearestScript : MonoBehaviour 6{ 7 GameObject nearestStone = null; 8 public GameObject[] stones; 9 10 // Use this for initialization 11 void Start() 12 { 13 14 } 15 16 // Update is called once per frame 17 void Update() 18 { 19 measure(); 20 21 } 22 23 void measure() 24 { 25 //ここが一回目を判定してくれない 26 if (Stone.rb.IsSleeping() && Stone.Ika)//投げた後かつStoneが止まったらtrue 27 { 28 29 GameObject[] stones = GameObject.FindGameObjectsWithTag("stone"); 30 31 float minDis = 100f; 32 foreach (GameObject stone in stones) 33 { 34 float dis = Vector3.Distance(transform.position, stone.transform.position); 35 if (dis < minDis) 36 { 37 Debug.Log(dis); 38 minDis = dis; 39 nearestStone = stone; 40 } 41 } 42 Debug.Log(nearestStone); 43 } 44 } 45 46} 47
試したこと
エラーメッセージは出ず、二投目からはしっかりと判定されます。
stones配列には赤と青のストーンオブジェクトを初期から入れています。
補足情報(FW/ツールのバージョンなど)
Unity 2017.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。