オブジェクトを縦にランダムに出すコードは検索でたくさん出てくるのですが、横にランダムに出すコードは数が少なく、縦にランダムに出すコードを少しいじってみたいと思います。
しかし、うまくいきません。
スクリプトはmovingObjというからのオブジェクトにアタッチしています。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CacaoController : MonoBehaviour { 6 public GameObject CacaoPrefab; 7 public GameObject movingObj; 8 Rigidbody2D rb; 9 10 void Start (){ 11 StartCoroutine (MoveMap ()); 12 StartCoroutine (SetCacao ()); 13 rb = GetComponent<Rigidbody2D>(); 14 } 15 16 void Update () { 17 } 18 19 IEnumerator SetCacao (){ 20 while (true) { 21 Vector3 pos = new Vector3 (0f, Random.Range(3.5f, -3.5f), 0); 22 GameObject tree = Instantiate (CacaoPrefab, pos, transform.rotation) as GameObject; 23 tree.transform.parent = movingObj.transform; 24 yield return new WaitForSeconds (1.5f); 25 } 26 } 27 28 IEnumerator MoveMap (){ 29 while (true) { 30 Vector3 pos = movingObj.transform.position; 31 pos.y -= -3 * Time.deltaTime; 32 movingObj.transform.position = pos; 33 yield return 0; 34 } 35 } 36}
↓ここの部分を変えたら横にランダムに出すことができると思うのですが、いまいち分かりません。
ご教授願います。
c#
1IEnumerator SetCacao (){ 2 while (true) { 3 Vector3 pos = new Vector3 (0f, Random.Range(3.5f, -3.5f), 0); 4 GameObject tree = Instantiate (CacaoPrefab, pos, transform.rotation) as GameObject; 5 tree.transform.parent = movingObj.transform; 6 yield return new WaitForSeconds (1.5f); 7 } 8 } 9 10 IEnumerator MoveMap (){ 11 while (true) { 12 Vector3 pos = movingObj.transform.position; 13 pos.y -= -3 * Time.deltaTime; 14 movingObj.transform.position = pos; 15 yield return 0; 16 } 17 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/14 07:22