前提・実現したいこと
簡単なビリヤードゲームをUnityで作ろうとしています。
まずは手玉がほかのボール(1~9)に当たった瞬間に、各ボールが弾くという処理を実装したいです。
発生している問題・エラーメッセージ
9つのボールをくっつけた状態でゲームを開始すると、再生ボタンを押した瞬間にそれぞれの方向にボールが飛んで行ってしまいます。
該当のソースコード
C#
1// 手玉にアタッチするスクリプト 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class MyBallMove : MonoBehaviour { 7 8 public float moveSpeed; 9 private Rigidbody rb; 10 11 // Use this for initialization 12 void Start () { 13 this.GetComponent<Rigidbody>().AddForce( 14 (transform.forward + transform.right) * moveSpeed, 15 ForceMode.VelocityChange); 16 } 17 18 // Update is called once per frame 19 void Update () { 20 21 22 } 23 24 void OnCollisionEnter(Collision other) 25 { 26 //Ballタグは9つのボールのタグ 27 if (other.gameObject.CompareTag("Ball")){ 28 this.GetComponent<Rigidbody>().AddForce( 29 (transform.forward + transform.right) * moveSpeed, 30 ForceMode.VelocityChange); 31 } 32 } 33}
//1~9のボールにアタッチするスクリプト using System.Collections; using System.Collections.Generic; using UnityEngine; public class ReboundBall : MonoBehaviour { private float moveSpeed = 2.0f; private GameObject targetBall; // Use this for initialization void Start () { GameObject.Find("One").SetActive(false); GameObject.Find("Two").SetActive(false); GameObject.Find("Three").SetActive(false); GameObject.Find("Four").SetActive(false); GameObject.Find("Five").SetActive(false); GameObject.Find("Six").SetActive(false); GameObject.Find("Seven").SetActive(false); GameObject.Find("Eight").SetActive(false); GameObject.Find("Nine").SetActive(false); } // Update is called once per frame void Update () { } void OnCollisionEnter(Collision other) { if (other.gameObject.CompareTag("Ball")) { this.GetComponent<Rigidbody>().AddForce( (transform.forward + transform.right) * moveSpeed, ForceMode.VelocityChange); } } }
試したこと
9つのボールを非アクティブにし、手玉が当たった瞬間にアクティブ状態にすれば手玉が当たった瞬間に各ボールが弾かれる動きをするか
と思いましたが、GameObject.Find("ボールの名前").SetActive(false);
では、再生ボタンを押した瞬間にボールが消えてしまい、うまくいきません。
補足情報(FW/ツールのバージョンなど)
基礎的なことかもしれませんが、ご教示頂きたいです。
宜しくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。