###行いたいこと
温度センサーを用いて温度が閾値を超えた時にunity側のobjectを動かすために、Arduinoとunityをシリアル通信させて、温度が閾値(thresold)をこえた時にy軸が0.5上昇して閾値を下回ったら元の位置に戻るということまでは下記のコードで行えるようにしました。そこで、温度が閾値を超えている時にはy軸が0.5ずつ上昇していき閾値を下回ったらその座標から動かないというコードを教えてほしいです。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SerialCube : MonoBehaviour
{
public SerialHandler serialHandler; public Text sensorText; public float threshold = 40f; public Vector3 move = new Vector3(0f, 0.5f, 0f); private Vector3 initialPosition; void Start() { serialHandler.OnDataReceived += OnDataReceived; initialPosition = transform.localPosition; } /* * シリアルを受け取った時の処理 */ void OnDataReceived(string message) { var data = message.Split( new string[] { "\t" }, System.StringSplitOptions.None); if (data.Length < 2) return; sensorText.text = "ondo:" + message; try { var temperature = float.Parse(data[0]); if (temperature > threshold) { transform.localPosition = initialPosition + move; } else { transform.localPosition = initialPosition; } } catch (System.Exception e) { Debug.LogWarning(e.Message); } }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。