実現したいこと
物体1,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}
をassetする。
物体2のy座標には物体1の現在のy座標にcsvファイル(x,y)のyに対応する値を足した値を滑らかに動くよう出力する。
前提
・csvファイルの1列目がx軸,2列目がy軸に相当している。(1列目は0.1刻み)
・今回は簡単のため、y=sin(x)としている
・必要があれば物体1のコードも変更してください
発生している問題・エラーメッセージ
物体1の現在の座標とcsvファイル(x,y)のyに対応する値を足した値を足し合わせた値を物体2に投影することができない。
該当のソースコード
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 newPosition.y = transform.position.y; // Keep the Y position from PlayerMovement 41 transform.position = newPosition; 42 43 // 次の点に到達した場合、次の点を設定してtをリセット 44 if (t >= 1f) 45 { 46 currentIndex = nextIndex; 47 t = 0f; 48 } 49 } 50 51 void ReadDataFromCSV() 52 { 53 if (csvFile == null) 54 { 55 Debug.LogError("CSVファイルが指定されていません。"); 56 return; 57 } 58 59 string[] lines = csvFile.text.Split('\n'); 60 61 for (int i = 1; i < lines.Length; i++) // 1行目はヘッダーなのでスキップ 62 { 63 string[] values = lines[i].Split(','); 64 if (values.Length < 2) continue; // 2列目がない行をスキップ 65 66 if (float.TryParse(values[0], out float xData) && float.TryParse(values[1], out float yData)) 67 { 68 xDataPoints.Add(xData); 69 yDataPoints.Add(yData); 70 } 71 else 72 { 73 Debug.LogWarning("データの読み取りに失敗しました: " + lines[i]); 74 } 75 } 76 } 77 78 // Add a method to update the Y position from PlayerMovement 79 public void UpdateBackgroundYPosition(float newY) 80 { 81 Vector3 currentPosition = transform.position; 82 currentPosition.y = newY; 83 transform.position = currentPosition; 84 } 85}
試したこと
物体1の現在の座標とcsvファイル(x,y)のyに対応する値を足した値を足し合わせようとした

0 コメント