前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/23 09:04