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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

0回答

521閲覧

Unityのライブラリのエラーについて(Vehicleの改造)

HiroPokeHero

総合スコア45

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

0クリップ

投稿2019/09/01 10:35

編集2022/01/12 10:55

前提・実現したいこと

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

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問