前提・実現したいこと
カーリングゲームを作成しているのですが、一投目と二投目で石の飛距離が変わってしまう
問題を解決したいです。
発生している問題・エラーメッセージ
赤が一投目、青が二投目なのですが、写真のように飛距離が
全く違っています。
三投目に関しては奥の壁に当たってしまいます。
エラーも出ていない状況です。
該当のソースコード
Stone.csは石に
NearestScript.csは真ん中の白いSphereに入れています。
Stone.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Stone : MonoBehaviour 6{ 7 public static Rigidbody rb; 8 public static bool Ika = false; 9 10 bool straight = false; 11 bool right = false; 12 bool left = false; 13 Vector3 speed; 14 15 public GameObject isi; 16 // Use this for initialization 17 void Start() 18 { 19 rb = GetComponent<Rigidbody>(); 20 } 21 22 void Update() 23 { 24 Debug.Log(speed); 25 speed = rb.velocity; 26 if (Input.GetKeyDown("up") ) 27 { 28 straight = true; 29 Ika = true; 30 } 31 32 if (Input.GetKeyDown("right")) 33 { 34 right = true; 35 } 36 if (Input.GetKeyDown("left")) 37 { 38 left = true; 39 } 40 } 41 42 // Update is called once per frame 43 void FixedUpdate() 44 { 45 if (straight) 46 { 47 rb.velocity = transform.forward * 30f;//石を前に飛ばす 48 straight = false; 49 } 50 51 if (right) 52 { 53 rb.AddForce(Vector3.right *speed.magnitude* 1f); 54 right = false; 55 } 56 if (left) 57 { 58 rb.AddForce(Vector3.left * speed.magnitude * 1f); 59 left = false; 60 } 61 if (rb.IsSleeping() && Ika) 62 { 63 Vector3 pos = transform.position; 64 pos.x = 0; 65 pos.y = 0; 66 pos.z = -65; 67 Instantiate(isi, pos, transform.rotation); 68 Ika = false; 69 gameObject.GetComponent<Stone>().enabled = false; 70 } 71 } 72 private void OnCollisionEnter(Collision other) 73 { 74 if(Ika && other.gameObject.CompareTag("wall")) 75 { 76 Vector3 pos = transform.position; 77 pos.x = 0; 78 pos.y = 0; 79 pos.z = -65; 80 Instantiate(isi, pos, transform.rotation); 81 Ika = false; 82 } 83 } 84} 85 86
カーリングの距離判定
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 11 // Use this for initialization 12 void Start() 13 { 14 15 } 16 void Update() 17 { 18 measure(); 19 score(); 20 } 21 // Update is called once per frame 22 void measure() 23 { 24 if (Stone.rb.IsSleeping() && Stone.Ika) 25 { 26 GameObject[] stones = GameObject.FindGameObjectsWithTag("stone"); 27 float minDis = 100f; 28 foreach (GameObject stone in stones) 29 { 30 float dis = Vector3.Distance(transform.position, stone.transform.position); 31 if (dis < minDis) 32 { 33 minDis = dis; 34 nearestStone = stone; 35 } 36 } 37 } 38 } 39 40 void score() 41 { 42 int point = 0; 43 if(Stone.count == 6) 44 { 45 if (nearestStone.name.Contains("aka")) 46 { 47 GameObject[] stones = GameObject.FindGameObjectsWithTag("stone"); 48 float minDis = 1000f; 49 foreach (GameObject stone in stones) 50 { 51 float dis = Vector3.Distance(transform.position, stone.transform.position); 52 if (dis < minDis && stone.name.Contains("ao")) 53 { 54 minDis = dis; 55 } 56 } 57 58 foreach (GameObject stone in stones) 59 { 60 float dis = Vector3.Distance(transform.position, stone.transform.position); 61 if (dis < minDis && stone.name.Contains("aka")) 62 { 63 point++; 64 } 65 } 66 Debug.Log(point); 67 } 68 69 if (nearestStone.name.Contains("ao")) 70 { 71 GameObject[] stones = GameObject.FindGameObjectsWithTag("stone"); 72 float minDis = 1000f; 73 foreach (GameObject stone in stones) 74 { 75 float dis = Vector3.Distance(transform.position, stone.transform.position); 76 if (dis < minDis && stone.name.Contains("aka")) 77 { 78 minDis = dis; 79 } 80 } 81 82 foreach (GameObject stone in stones) 83 { 84 float dis = Vector3.Distance(transform.position, stone.transform.position); 85 if (dis < minDis && stone.name.Contains("ao")) 86 { 87 point++; 88 } 89 } 90 Debug.Log(point); 91 } 92 } 93 } 94 95} 96
投げた石が止まったときに次の石を生成したいため、bool型変数Ikaを使用しています。
投げた時(上キーを押したとき)にIkaをtrue、かつその投げた石が止まったとき(IsSleeping())に次の石を生成したいために利用しています。(使わないと、生成したときに、IsSleeping()がtrueになってしまう)
staticなのはNearestScriptで変数を利用したいがためにのみ使っています。
キー入力では左右は石の左右の動作、上は真っすぐの動作を記載しています。
今回は真っすぐの動作が期待通りの動作をしていない状況です、
FixedUpdate内で投げた時のrb.velocityの大きさを確認しましたが、
大きさはすべて(0, 0, 30)でした。
一投目の石はInstantiateではなく、元から置いてあります。
試したこと
【追記】
距離が変わる原因なのですが、すでに投げてある石のrigidbodyが投げる石のrigidbodyに加算されているようでした。すでに投げてある石のrigidbodyを除くと同じ飛距離を飛んでくれました。
原因はわかったのですが、解決策がわからずに今困っている状況です。
補足情報(FW/ツールのバージョンなど)
Unity2017.4
回答2件
あなたの回答
tips
プレビュー