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

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

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

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

Unity3D

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

Unity

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

Q&A

解決済

1回答

2980閲覧

Unity内の3DオブジェクトにSendKeysクラスで命令したい

tia_0223

総合スコア5

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2020/01/22 13:39

編集2020/01/22 13:45

前提・実現したいこと

unity内に置いて、プレイヤーが操作できる3Dオブジェクト(具体的にはstandard assetsのcar)
を、特定の座標に到達した時点で自動でブレーキをかけて停止させたい。

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

SendKeysクラスを用いて、特定の座標到達時にスペースボタン(ブレーキがかかるボタン)が押されるようにすることでブレーキをかけようと考えているのですが、次のようなエラーメッセージが出てしまいブレーキがかからない(spaceキーが押された判定にならない?)です。どうすればよいでしょうか

ArgumentException: SendKeys string {0} is not valid. Parameter name: {SPACE} System.Windows.Forms.SendKeys.Parse (System.String key_string) (at <73410c649db2499885382ad2ba89fba1>:0) System.Windows.Forms.SendKeys.Send (System.String keys) (at <73410c649db2499885382ad2ba89fba1>:0) carmove.Update () (at Assets/carmove.cs:24)

該当のソースコード

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Windows.Forms; 5 6public class carmove : MonoBehaviour 7{ 8 public GameObject Car; 9 float point_z = 20.0f; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 Car = GameObject.Find("Car"); 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 Vector3 position = Car.transform.position; 21 22 if (position.z >= point_z) 23 { 24 SendKeys.Send("{SPACE}"); 25 } 26 } 27} 28

試したこと

Send()内にスペースキー以外のキーを入れて試行してみたところ、エラーは出なかったのですがオブジェクトの運動には特に変化が見られませんでした。

補足情報(FW/ツールのバージョンなど)

当方unityの扱い、c♯言語の扱い等に慣れておらず、非常に初歩的なミスである可能性も高いのですが、ご教授いただければ幸いです。
また、これ以外の手段で容易にブレーキをかけることができるのであれば、そちらのやり方について教えて頂きたいです。

オブジェクトの、恐らく関係しているであろう部分のスクリプトも添付させて頂きます

c#

1using System; 2using UnityEngine; 3using UnityStandardAssets.CrossPlatformInput; 4 5namespace UnityStandardAssets.Vehicles.Car 6{ 7 [RequireComponent(typeof (CarController))] 8 public class CarUserControl : MonoBehaviour 9 { 10 private CarController m_Car; // the car controller we want to use 11 12 13 private void Awake() 14 { 15 // get the car controller 16 m_Car = GetComponent<CarController>(); 17 } 18 19 20 private void FixedUpdate() 21 { 22 // pass the input to the car! 23 float h = CrossPlatformInputManager.GetAxis("Horizontal"); 24 float v = CrossPlatformInputManager.GetAxis("Vertical"); 25#if !MOBILE_INPUT 26 float handbrake = CrossPlatformInputManager.GetAxis("Jump"); 27 m_Car.Move(h, v, v, handbrake); 28#else 29 m_Car.Move(h, v, v, 0f); 30#endif 31 } 32 } 33} 34

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

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

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

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

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

guest

回答1

0

ベストアンサー

リファレンスの表にスペースは載っていないようですので、SendKeys.Send(" ");でいいのかもしれませんね。

ですがUpdate内でSendKeysによるニセのキー入力を行っても、UnityのInputには通じないかもしれません。それにSystem.Windows.Formsの機能を使おうとすると、プラットフォーム依存性の関係でトラブルの原因になりそうな気もします。
代案として、条件を満たしたらCarUserControlをオフにし、代わりに自分自身に制御を移すというのはどうでしょうか。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityStandardAssets.CrossPlatformInput; 5using UnityStandardAssets.Vehicles.Car; 6 7public class carmove : MonoBehaviour 8{ 9 public GameObject Car; 10 float point_z = 20.0f; 11 12 CarUserControl carUserControl; 13 CarController carController; 14 15 // Start is called before the first frame update 16 void Start() 17 { 18 Car = GameObject.Find("Car"); 19 carUserControl = Car.GetComponent<CarUserControl>(); 20 carController = Car.GetComponent<CarController>(); 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 Vector3 position = Car.transform.position; 27 28 if (position.z >= point_z) 29 { 30 carUserControl.enabled = false; 31 } 32 } 33 34 private void FixedUpdate() 35 { 36 // CarUserControlが無効化されているなら、代わりにこのスクリプトで車をコントロールする 37 if (!carUserControl.enabled) 38 { 39 // ハンドブレーキを引きっぱなしにし、入力はハンドル操作のみ受け付ける 40 float h = CrossPlatformInputManager.GetAxis("Horizontal"); 41 carController.Move(h, 0.0f, 0.0f, 1.0f); 42 } 43 } 44}

投稿2020/01/22 22:21

編集2020/01/23 01:15
Bongo

総合スコア10807

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

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

tia_0223

2020/01/23 09:04

スクリプトを書き換えた所、無事ブレーキをかけることに成功しました。本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問