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

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

新規登録して質問してみよう
ただいま回答率
85.47%
Unity

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

Q&A

0回答

158閲覧

「unity」物体1のy座標の値とcsvファイルの値x軸に該当するy軸を足し合わせた値を物体2に投影させたい

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2023/09/27 06:53

実現したいこと

・物体1のy座標の値とcsvファイルの値x軸に該当するy軸を足し合わせた値を物体2に投影させたい

前提

物体1のコードは以下の通りです。

unity

1using UnityEngine; 2 3public class PlayerMovement : MonoBehaviour 4{ 5 public float moveSpeed = 5.0f; // 移動速度を調整できるようにします 6 7 void Update() 8 { 9 float verticalInput = Input.GetAxisRaw("Vertical"); // Input.GetAxisRaw を使用 10 11 Debug.Log("Vertical Input: " + verticalInput); // デバッグログ 12 13 // Y軸の移動をキーボードの上下キーに連動させる 14 Vector3 movement = new Vector3(0f, verticalInput, 0f) * moveSpeed * Time.deltaTime; 15 transform.Translate(movement); 16 } 17}

また、以下のコードを物体2にアッセトすることで物体1のy軸を同じにすることはできました。

unity

1using UnityEngine; 2 3public class UpdateAndApplyYPosition : MonoBehaviour 4{ 5 public Transform object1; // 物体1のTransformコンポーネントをアサイン 6 public Transform object2; // 物体2のTransformコンポーネントをアサイン 7 8 private float yPosition1; // 物体1のy座標を保存する変数 9 10 void Update() 11 { 12 // 物体1のy座標を随時更新 13 yPosition1 = object1.position.y; 14 15 // 物体2に物体1のy座標を適用 16 Vector3 newPosition2 = object2.position; 17 newPosition2.y = yPosition1; 18 object2.position = newPosition2; 19 } 20}

 
csvファイルは2列あり、1列目はx座標で0.1刻み、2列目はy座標であり、単体の場合には以下のコードで実行することができた。

unity

1using UnityEngine; 2using System.Collections.Generic; 3using System.IO; 4 5public class Background : MonoBehaviour 6{ 7 public TextAsset csvFile; // csvファイルのテキストアセット 8 public float graphScaleX = 1f; // x軸方向のグラフの大きさ(スケール) 9 public float graphScaleY = 1f; // y軸方向のグラフの大きさ(スケール) 10 public float moveSpeed = 1f; // グラフの点が動く速度 11 12 private List<float> xDataPoints = new List<float>(); 13 private List<float> yDataPoints = new List<float>(); 14 private int currentIndex = 0; 15 private float t = 0f; 16 17 void Start() 18 { 19 ReadDataFromCSV(); 20 } 21 22 void Update() 23 { 24 if (xDataPoints.Count == 0) return; 25 26 // 現在の点の座標を計算 27 float currentX = xDataPoints[currentIndex] * graphScaleX; 28 float currentY = yDataPoints[currentIndex] * graphScaleY; 29 Vector3 currentPoint = new Vector3(currentX, currentY, 0f); 30 31 // 次の点の座標を計算 32 int nextIndex = (currentIndex + 1) % xDataPoints.Count; 33 float nextX = xDataPoints[nextIndex] * graphScaleX; 34 float nextY = yDataPoints[nextIndex] * graphScaleY; 35 Vector3 nextPoint = new Vector3(nextX, nextY, 0f); 36 37 // 点を移動する 38 t += Time.deltaTime * moveSpeed; 39 Vector3 newPosition = Vector3.Lerp(currentPoint, nextPoint, t); 40 transform.position = newPosition; 41 42 // 次の点に到達した場合、次の点を設定してtをリセット 43 if (t >= 1f) 44 { 45 currentIndex = nextIndex; 46 t = 0f; 47 } 48 } 49 50 void ReadDataFromCSV() 51 { 52 if (csvFile == null) 53 { 54 Debug.LogError("CSVファイルが指定されていません。"); 55 return; 56 } 57 58 string[] lines = csvFile.text.Split('\n'); 59 60 for (int i = 1; i < lines.Length; i++) // 1行目はヘッダーなのでスキップ 61 { 62 string[] values = lines[i].Split(','); 63 if (values.Length < 2) continue; // 2列目がない行をスキップ 64 65 if (float.TryParse(values[0], out float xData) && float.TryParse(values[1], out float yData)) 66 { 67 xDataPoints.Add(xData); 68 yDataPoints.Add(yData); 69 } 70 else 71 { 72 Debug.LogWarning("データの読み取りに失敗しました: " + lines[i]); 73 } 74 } 75 } 76} 77

発生している問題・エラーメッセージ

UpdateAndApplyYPositionとBackgroundのコードを合わせるとy=0から動かなくなる。

試したこと

unity

1using UnityEngine; 2using System.Collections.Generic; 3using System.IO; 4 5public class Background : MonoBehaviour 6{ 7 public TextAsset csvFile; // csvファイルのテキストアセット 8 public float graphScaleX = 1f; // x軸方向のグラフの大きさ(スケール) 9 public float graphScaleY = 1f; // y軸方向のグラフの大きさ(スケール) 10 public float moveSpeed = 1f; // グラフの点が動く速度 11 12 public Transform object1; // 物体1のTransformコンポーネントをアサイン 13 14 private List<float> xDataPoints = new List<float>(); 15 private List<float> yDataPoints = new List<float>(); 16 private int currentIndex = 0; 17 private float t = 0f; 18 19 void Start() 20 { 21 ReadDataFromCSV(); 22 } 23 24 void Update() 25 { 26 if (xDataPoints.Count == 0) return; 27 28 // 現在の点の座標を計算 29 float currentX = xDataPoints[currentIndex] * graphScaleX; 30 float currentY = yDataPoints[currentIndex] * graphScaleY; 31 Vector3 currentPoint = new Vector3(currentX, currentY, 0f); 32 33 // 次の点の座標を計算 34 int nextIndex = (currentIndex + 1) % xDataPoints.Count; 35 float nextX = xDataPoints[nextIndex] * graphScaleX; 36 float nextY = yDataPoints[nextIndex] * graphScaleY; 37 Vector3 nextPoint = new Vector3(nextX, nextY, 0f); 38 39 // 点を移動する 40 t += Time.deltaTime * moveSpeed; 41 Vector3 newPosition = Vector3.Lerp(currentPoint, nextPoint, t); 42 transform.position = newPosition; 43 44 // 次の点に到達した場合、次の点を設定してtをリセット 45 if (t >= 1f) 46 { 47 currentIndex = nextIndex; 48 t = 0f; 49 } 50 } 51 52 void ReadDataFromCSV() 53 { 54 if (csvFile == null) 55 { 56 Debug.LogError("CSVファイルが指定されていません。"); 57 return; 58 } 59 60 string[] lines = csvFile.text.Split('\n'); 61 62 for (int i = 1; i < lines.Length; i++) // 1行目はヘッダーなのでスキップ 63 { 64 string[] values = lines[i].Split(','); 65 if (values.Length < 2) continue; // 2列目がない行をスキップ 66 67 if (float.TryParse(values[0], out float xData) && float.TryParse(values[1], out float yData)) 68 { 69 // 物体1のy座標を取得 70 float yPosition1 = object1.position.y; 71 72 // yDataに物体1のy座標を加算してからリストに追加 73 yData += yPosition1; 74 75 xDataPoints.Add(xData); 76 yDataPoints.Add(yData); 77 } 78 else 79 { 80 Debug.LogWarning("データの読み取りに失敗しました: " + lines[i]); 81 } 82 } 83 } 84} 85

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問