質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

920閲覧

Prefabから作った複数のオブジェクトを移動させようとするがうまくいかない

hageandatsu

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/11/05 04:33

LineRendererでグラフィックを生成するオブジェクトをPrefab(script: Line.cs)からLineGenerator.cs(GameObjectにアタッチされている)で生成し
そのオブジェクトのLine.csにfloat[] x,yで座標を送ってグラフィックを移動させています。
生成時の座標からfloat[]で送った座標に移動させるときにMathf.Lerpでアニメーションさせています。

LineGenerator.csから送る座標は全部で四つのグラフィック分あり、今後増えることを考えて一度List<>に格納しています。
マウスボタンを押すたびに一つ生成し座標を送っています。

問題はアニメーションのためのMathf.Lerpをつけるとprivateなはずの値が干渉してしまうのか四つのグラフィックが全部真ん中に集まってしまうことです。Playを一時停止してグラフィックのオブジェクトを一つずつ消すと与えた座標の位置に近づいていきます。オブジェクトが一つになると正しい位置に収まります。

アニメーションをつけたままfloat[]で与えた座標の位置にすべてのグラフィックが収まるようにしたいのですがどうしたらよいでしょうか?
また、もしかするとLineGeneratorから座標を送るシステムが悪いのでしょうか? 別のクラスに座標計算メソッドを作ってグラフィックのオブジェクトから呼び出す形にした方が良いでしょうか?

こちらがLineGenerator.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class LineGenerator : MonoBehaviour 6{ 7 [SerializeField] public GameObject LinePrefab; 8 [SerializeField] public GameObject panel; 9 10 public int count = -1; 11 public static List<List<float>> listx = new List<List<float>>(); 12 public static List<List<float>> listy = new List<List<float>>(); 13 public static List<GameObject> boxes = new List<GameObject>(); 14 15 16 private Line LineScript; 17 18 // Start is called before the first frame update 19 void Start() 20 { 21 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 28 if (Input.GetMouseButtonDown(0)) 29 { 30 31 count++; 32 GameObject Line = Instantiate(LinePrefab) as GameObject; 33 Line.transform.SetParent(panel.transform, false); 34 Line.GetComponent<Line>().indexNo = count; 35 Line.name = "Box" + count; 36 37 boxes.Add(Line); 38 39 // 生成する枠の正しい位置を計算するメソッド 40 // Calcurate(); 41 42 float[] vertexX = CalcurateX(count); 43 float[] vertexY = CalcurateY(count); 44 45 46 boxes[count].transform.GetComponent<Line>().x = vertexX; 47 boxes[count].transform.GetComponent<Line>().y = vertexY; 48 49 } 50 } 51 52 public float[] CalcurateX(int n) 53 { 54 listx.Add(new List<float>()); 55 //仮に手打ち。今後は自動化とメソッドで適正な位置に生成 56 listx[0].Add(-8.8f); 57 listx[0].Add(-4.4f); 58 listx[0].Add(-4.4f); 59 listx[0].Add(-8.8f); 60 listx.Add(new List<float>()); 61 listx[1].Add(-4.4f); 62 listx[1].Add(0f); 63 listx[1].Add(0f); 64 listx[1].Add(-4.4f); 65 listx.Add(new List<float>()); 66 listx[2].Add(0f); 67 listx[2].Add(4.4f); 68 listx[2].Add(4.4f); 69 listx[2].Add(0f); 70 listx.Add(new List<float>()); 71 listx[3].Add(4.4f); 72 listx[3].Add(8.8f); 73 listx[3].Add(8.8f); 74 listx[3].Add(4.4f); 75 listx.Add(new List<float>()); 76 77 78 float a = listx[n][0]; 79 float b = listx[n][1]; 80 float c = listx[n][2]; 81 float d = listx[n][3]; 82 float[] vertexX = new float[] { a, b, c, d }; 83 return vertexX; 84 85 } 86 87 public float[] CalcurateY(int p) 88 { 89 listy.Add(new List<float>()); 90 //仮に手打ち。今後は自動化とメソッドで適正な位置に生成 91 listy[0].Add(4.9f); 92 listy[0].Add(4.9f); 93 listy[0].Add(-4.9f); 94 listy[0].Add(-4.9f); 95 listy.Add(new List<float>()); 96 listy[1].Add(4.9f); 97 listy[1].Add(4.9f); 98 listy[1].Add(-4.9f); 99 listy[1].Add(-4.9f); 100 listy.Add(new List<float>()); 101 listy[2].Add(4.9f); 102 listy[2].Add(4.9f); 103 listy[2].Add(-4.9f); 104 listy[2].Add(-4.9f); 105 listy.Add(new List<float>()); 106 listy[3].Add(4.9f); 107 listy[3].Add(4.9f); 108 listy[3].Add(-4.9f); 109 listy[3].Add(-4.9f); 110 listy.Add(new List<float>()); 111 112 113 float a = listy[p][0]; 114 float b = listy[p][1]; 115 float c = listy[p][2]; 116 float d = listy[p][3]; 117 float[] vertexY = new float[] { a, b, c, d }; 118 return vertexY; 119 }

こちらがLine.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Line : MonoBehaviour 6{ 7 public int indexNo; 8 private int count; 9 10 //public float x, y; 11 12 public float[] x { get; set; } 13 public float[] y { get; set; } 14 15 private static float a, b, c, d, e, f, g, h = 0; 16 17 public float speed = 1.0f; 18 19 private static Vector3 pos0; 20 private static Vector3 pos1; 21 private static Vector3 pos2; 22 private static Vector3 pos3; 23 24 private static Vector3 endpos0; 25 private static Vector3 endpos1; 26 private static Vector3 endpos2; 27 private static Vector3 endpos3; 28 29 30 // Start is called before the first frame update 31 void Start() 32 { 33 LineRenderer renderer = gameObject.GetComponent<LineRenderer>(); 34 // 線の幅 35 renderer.SetWidth(0.1f, 0.1f); 36 // 頂点の数 37 renderer.SetVertexCount(4); 38 // 頂点を設定 39 40 a = -8.8f; 41 b = 8.8f; 42 c = 8.8f; 43 d = -8.8f; 44 45 e = 4.9f; 46 f = 4.9f; 47 g = -4.9f; 48 h = -4.9f; 49 50 pos0 = new Vector3(a, e, 0f); 51 pos1 = new Vector3(b, f, 0f); 52 pos2 = new Vector3(c, g, 0f); 53 pos3 = new Vector3(d, h, 0f); 54 55 renderer.SetPosition(0, pos0); 56 renderer.SetPosition(1, pos1); 57 renderer.SetPosition(2, pos2); 58 renderer.SetPosition(3, pos3); 59 renderer.loop = true; 60 } 61 62 // Update is called once per frame 63 void Update() 64 { 65 LineRenderer renderer = gameObject.GetComponent<LineRenderer>(); 66 // 線の幅 67 renderer.SetWidth(0.1f, 0.1f); 68 // 頂点の数 69 renderer.SetVertexCount(4); 70 //頂点の線形移動 71 72 73 //どうやらこの部分が悪さをしているようだ。 74 75 a = Mathf.Lerp(a, x[0], 0.1f); 76 b = Mathf.Lerp(b, x[1], 0.1f); 77 c = Mathf.Lerp(c, x[2], 0.1f); 78 d = Mathf.Lerp(d, x[3], 0.1f); 79 80 e = Mathf.Lerp(e, y[0], 0.1f); 81 f = Mathf.Lerp(f, y[1], 0.1f); 82 g = Mathf.Lerp(g, y[2], 0.1f); 83 h = Mathf.Lerp(h, y[3], 0.1f); 84 85 Vector3 endpos0 = new Vector3(a, e, 0f); 86 Vector3 endpos1 = new Vector3(b, f, 0f); 87 Vector3 endpos2 = new Vector3(c, g, 0f); 88 Vector3 endpos3 = new Vector3(d, h, 0f); 89 90 //Lerpぬきのこちらは正常に動く 91 /* 92 Vector3 endpos0 = new Vector3(x[0], y[0], 0f); 93 Vector3 endpos1 = new Vector3(x[1], y[1], 0f); 94 Vector3 endpos2 = new Vector3(x[2], y[2], 0f); 95 Vector3 endpos3 = new Vector3(x[3], y[3], 0f); 96 */ 97 // 頂点を設定 98 renderer.SetPosition(0, endpos0); 99 renderer.SetPosition(1, endpos1); 100 renderer.SetPosition(2, endpos2); 101 renderer.SetPosition(3, endpos3); 102 renderer.loop = true; 103 } 104} 105

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2020/11/06 00:27

「Debug.Log」を使って各値がどうなっているのか確認してみてください。 ちなみに2値を渡したいならx,yを別々の配列にせずVector2型の配列で渡す等の方法もあるので検討してみてください。
hageandatsu

2020/11/06 06:15

コメントありがとうございます。 Line.csを書き換えて、取得したa~hを一旦A~Hに代入してから計算するようにしたところ問題が解決しました。多分float[] x,yを送るたびにa~hが書き換えられてしまったのでしょう(ループして代入される計算をupdateで行っていたため)。vector2型で送る方がスマートな気がするので今後書き換えようと思いますがこの場合はmathf.lerpが使えなくなりますよね……vector3のlerpをつかったところ実は画面の中央にすべてのグラフィックが消えていくバグがあってx,yで分けてmathf.lerpを使っているのです…
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問