前提・実現したいこと
StandardAssetsの車両モデルを利用させていただいて、
簡易的なドライブシミュレータを作ろうと思っています。
そこで、ユーザ入力をキーボードではなくシリアル通信で取得するつもりです。
前提として、
シリアル通信は正常に動作できることを別のプロジェクト(シリアル通信確認用)で確認しました。
また、そのシリアル通信のデータによってUnity内のオブジェクトを操作できることも確認できています。
また、以前に別のプロジェクトでVehicleを使っていたのでその中のCarUserControl.csを改造してエラーがなかったものをそのままコピペでもってきています。
なぜかうまくいくときもあります。
うまくいくときと行かないときの違いが正直わかりません。
発生している問題・エラーメッセージ
以下のようなエラーが発生し、ビルドができません。
Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?) Vehicle.Plugins Assets\Vehicles\Car\Scripts\CarUserControl.cs 15 Active Error CS0234 The type or namespace name 'Ports' does not exist in the namespace 'System.IO' (are you missing an assembly reference?) Vehicle.Plugins Assets\Vehicles\Car\Scripts\CarUserControl.cs 6 Active Error CS0246 The type or namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?) Vehicle.Plugins Assets\Vehicles\Car\Scripts\CarUserControl.cs 15 Active
該当のソースコード
using System; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; using System.Collections; using System.Collections.Generic; using System.IO.Ports; using System.Threading; namespace UnityStandardAssets.Vehicles.Car { [RequireComponent(typeof (CarController))] public class CarUserControl : MonoBehaviour { private CarController m_Car; // the car controller we want to use SerialPort SerPort = new SerialPort("COM3", 9600); string data; int ang1, ang2, ang3; float ang1f, ang2f, ang3f; private void Awake() { // get the car controller m_Car = GetComponent<CarController>(); if (SerPort.IsOpen) { SerPort.Close(); } else { SerPort.Open(); SerPort.ReadTimeout = 1000; } } private void FixedUpdate() { // pass the input to the car! float h = CrossPlatformInputManager.GetAxis("Horizontal"); float v = CrossPlatformInputManager.GetAxis("Vertical"); data = SerPort.ReadLine(); var datas = data.Split(','); var rb = GetComponent<Rigidbody>(); ang1 = Convert.ToInt32(datas[0], 16); ang2 = Convert.ToInt32(datas[1], 16); ang3 = Convert.ToInt32(datas[2], 16); ang1f = (float)(512 - ang1) / 512; // 1/300[deg] ang2f = (float)(512 - ang2) / 512; // [%] ang3f = (float)(512 - ang3) / 512; // [%] h = (float)(512 - ang1) / 512; v = (float)(512 - ang3) / 512; Debug.Log(h); Debug.Log(v); #if !MOBILE_INPUT float handbrake = CrossPlatformInputManager.GetAxis("Jump"); m_Car.Move(h, v, v, handbrake); #else m_Car.Move(h, v, v, 0f); #endif } } }
試したこと
Systemの下についているものが「必要ない」というメッセージが出るのでコメントアウトするなど。
using System; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; //using System.Collections; //using System.Collections.Generic; //using System.IO.Ports; //using System.Threading;
補足情報(FW/ツールのバージョンなど)
Windows10
Unity5.5.4
visualStudio2015Community
あなたの回答
tips
プレビュー