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

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

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

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

Unity

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

Leap Motion

Leap Motionは、Leap Motionによって開発、販売している、手のジェスチャーでパソコンを操作できるデバイスです。

Oculus Rift

Oculus Riftは、ゲームに特化した広視野角バーチャルリアリティヘッドマウントディスプレイ です。そのため、バーチャルリアリティ・ゲームで使用するのを第一目的として開発されています。

Q&A

0回答

623閲覧

LeapMotionでトラッキングさせる手のモデルの位置を、キー入力で操作したい

hoheinabachan

総合スコア6

C#

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

Unity

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

Leap Motion

Leap Motionは、Leap Motionによって開発、販売している、手のジェスチャーでパソコンを操作できるデバイスです。

Oculus Rift

Oculus Riftは、ゲームに特化した広視野角バーチャルリアリティヘッドマウントディスプレイ です。そのため、バーチャルリアリティ・ゲームで使用するのを第一目的として開発されています。

0グッド

0クリップ

投稿2019/07/05 08:21

編集2019/07/05 09:39

Unity5.6.3p2で、LeapMotionとOculus Riftを用いて実験をしています。

F1~F6、1~6、spaceキーを押すとLeapMotionで認識した左手のモデルの位置が少しずつ左右にズレるようになっています。

Oculus Riftを起動していない状態でUnityを動かすとGameビュー上では想定通りの動作をしていて問題ないのですが、Oculus Riftを起動した状態だとキーを押しても動きません(例えばF4を押しても動くときと動かない時がある)。

先輩からの引継ぎで、今は直接質問できないのでここで質問させていただきます。どこに問題があるのか見当がつかないのでとりあえず直接関わりがありそうなコードを貼りました。判断材料が足りないようでしたら適宜追加します。どうぞよろしくお願いいたします。

このようなエラーも出ます。Vector3が問題なのはわかるのですが…
NullReferenceException: Object reference not set to an instance of an object
RiggedFinger.Update () (at Assets/LeapMotion/Scripts/Hands/RiggedFinger.cs:77)

C#

1using UnityEngine; 2using System.Collections; 3using Leap; 4 5public class RiggedFinger : FingerModel 6{ 7 public bool deformPosition = false; 8 9 public Vector3 modelFingerPointing = Vector3.forward; 10 public Vector3 modelPalmFacing = -Vector3.up; 11 private GameObject centerObj; 12 13 public Quaternion Reorientation() 14 { 15 return Quaternion.Inverse(Quaternion.LookRotation(modelFingerPointing, -modelPalmFacing)); 16 } 17 18 /** Updates the bone rotations. */ 19 public override void UpdateFinger() 20 { 21 for (int i = 0; i < bones.Length; ++i) 22 { 23 if (bones[i] != null) 24 { 25 bones[i].rotation = GetBoneRotation(i) * Reorientation(); 26 if (deformPosition) 27 { 28 bones[i].position = GetBoneCenter(i); 29 } 30 } 31 } 32 } 33 private void Start() 34 { 35 print("2"); 36 centerObj = GameObject.Find("Brush1"); //試しのコメントアウト 37 38 centerObj.transform.position = new Vector3(centerObj.transform.position.x, centerObj.transform.position.y, centerObj.transform.position.z); 39 } 40 public void Update() 41 { 42 Hand righthand = GetLeapHand(); 43 //print(111); 44 45 if (righthand.IsRight) 46 { 47 Finger finger = GetLeapFinger(); 48 Finger.FingerType fType = fingerType; 49 if (fType == Finger.FingerType.TYPE_INDEX) 50 { 51 Vector3 pencilposition = this.bones[3].position; 52 53 Quaternion pencilrotation = GetBoneRotation(3) * Reorientation(); 54 pencilrotation = Quaternion.Euler(20, 0, 80); 55 56 57 Vector3 position = this.centerObj.transform.position; 58 centerObj.transform.position = new Vector3(pencilposition.x, pencilposition.y, pencilposition.z); 59 centerObj.transform.rotation = new Quaternion(pencilrotation.w, pencilrotation.x, pencilrotation.y, pencilrotation.z); 60 } 61 } 62 63 64 } 65 66}

C#

1RiggedHand.cs 2using UnityEngine; 3 using System.Collections; 4 using Leap; 5 6// Class to setup a rigged hand based on a model. 7public class LeapUtil 8{ 9 public static Vector3 ToPositionVector3(Vector position) 10 { 11 return new Vector3(position.x, position.y, -position.z); 12 } 13 14 public static Vector3 ToVector3(Vector v) 15 { 16 return new Vector3(v.x, v.y, v.z); 17 } 18 19 public static void LookAt(Transform t, Vector norlmal) 20 { 21 t.LookAt(t.position + ToPositionVector3(norlmal), Vector3.forward); 22 } 23 24} 25public class RiggedHand : HandModel { 26 float alpha = 0; 27 int beta = 1; 28 bool is_z_pressed = false; 29 bool is_k_pressed = false; 30 Vector3 stop; 31 32 Quaternion stop1; 33 Quaternion stop2; 34 public Vector3 modelFingerPointing = Vector3.forward; 35 public Vector3 modelPalmFacing = -Vector3.up; 36 private GameObject knifeObj; 37 38 public override void InitHand() { 39 print("1"); 40 UpdateHand(); 41 } 42 43 public Quaternion Reorientation() { 44 return Quaternion.Inverse(Quaternion.LookRotation(modelFingerPointing, -modelPalmFacing)); 45 } 46 47 public override void UpdateHand() 48 { 49 Hand rightgesture = GetLeapHand(); 50 51 52 if (rightgesture.IsLeft) 53 { 54 if (palm != null) 55 { 56 Vector3 position = GetPalmPosition(); 57 58 if (Input.GetKeyDown(KeyCode.F1)) 59 { 60 alpha = -0.3f; 61 //print("a"); 62 } 63 if (Input.GetKeyDown(KeyCode.F2)) 64 { 65 alpha = -0.25f; 66 } 67 if (Input.GetKeyDown(KeyCode.F3)) 68 { 69 alpha = -0.2f; 70 } 71 if (Input.GetKeyDown(KeyCode.F4)) 72 { 73 alpha = -0.15f; 74 } 75 if (Input.GetKeyDown(KeyCode.F5)) 76 { 77 alpha = -0.1f; 78 } 79 if (Input.GetKeyDown(KeyCode.F6)) 80 { 81 alpha = -0.05f; 82 } 83 if (Input.GetKeyDown(KeyCode.Space)) 84 { 85 alpha = 0; 86 } 87 if (Input.GetKeyDown(KeyCode.Alpha1)) 88 { 89 alpha = 0.05f; 90 } 91 if (Input.GetKeyDown(KeyCode.Alpha2)) 92 { 93 alpha = 0.1f; 94 } 95 if (Input.GetKeyDown(KeyCode.Alpha3)) 96 { 97 alpha = 0.15f; 98 } 99 if (Input.GetKeyDown(KeyCode.Alpha4)) 100 { 101 alpha = 0.2f; 102 } 103 if (Input.GetKeyDown(KeyCode.Alpha5)) 104 { 105 alpha = 0.25f; 106 } 107 if (Input.GetKeyDown(KeyCode.Alpha6)) 108 { 109 alpha = 0.3f; 110 } 111 112 if (Input.GetKeyDown(KeyCode.Z)) 113 { 114 115 116 117 118 Vector3 stop = new Vector3(palm.position.x, palm.position.y, palm.position.z); 119 120 Quaternion stop1 = GetPalmRotation() * Reorientation(); 121 Quaternion stop2 = GetArmRotation() * Reorientation(); 122 stop1 = Quaternion.Euler(0, -85, 190); 123 stop2 = Quaternion.Euler(0, -85, 180); 124 is_z_pressed = true; 125 //print("rubber hand position:"+stop.x); 126 127 128 palm.position = new Vector3(stop.x, stop.y, stop.z); 129 130 //palm.position = palm.transform.position; 131 132 palm.transform.rotation = new Quaternion(stop1.w, stop1.x, stop1.y, stop1.z); 133 forearm.rotation = new Quaternion(stop2.w, stop2.x, stop2.y, stop2.z); 134 //Instantiate(palm, newStop, palm.transform.rotation); 135 136 137 } 138 if (!is_z_pressed) //Zキーが押されていないなら 139 { 140 141 142 143 144 145 palm.position = new Vector3(position.x + alpha, position.y, position.z); 146 palm.rotation = GetPalmRotation() * Reorientation(); 147 if (forearm != null) 148 forearm.rotation = GetArmRotation() * Reorientation(); 149 150 for (int i = 0; i < fingers.Length; ++i) 151 { 152 if (fingers[i] != null) 153 { 154 fingers[i].fingerType = (Finger.FingerType)i; 155 fingers[i].UpdateFinger(); 156 } 157 } 158 159 } 160 161 } 162 163 164 } 165 if (rightgesture.IsRight) 166 { 167 if (palm != null) 168 { 169 Vector3 position = GetPalmPosition(); 170 171 if (Input.GetKeyDown(KeyCode.Q)) 172 { 173 alpha = 0.2f; 174 print("a"); 175 } 176 if (Input.GetKeyDown(KeyCode.W)) 177 { 178 alpha = 0.4f; 179 } 180 if (Input.GetKeyDown(KeyCode.E)) 181 { 182 alpha = 0.5f; 183 } 184 if (Input.GetKeyDown(KeyCode.Space)) 185 { 186 alpha = 0; 187 } 188 palm.position = new Vector3(position.x + alpha, position.y, position.z); 189 palm.rotation = GetPalmRotation() * Reorientation(); 190 } 191 192 if (forearm != null) 193 forearm.rotation = GetArmRotation() * Reorientation(); 194 195 for (int i = 0; i < fingers.Length; ++i) 196 { 197 if (fingers[i] != null) 198 { 199 fingers[i].fingerType = (Finger.FingerType)i; 200 fingers[i].UpdateFinger(); 201 } 202 } 203 } 204 } 205 private void Start() 206 { 207 208 knifeObj = GameObject.Find("knife"); 209 knifeObj.transform.position = new Vector3(knifeObj.transform.position.x, knifeObj.transform.position.y, knifeObj.transform.position.z); 210 211 } 212 public void Update() 213 { 214 //print(knifeobj.transform.position); 215 216 if (Input.GetKeyDown(KeyCode.K)) 217 { 218 Vector3 knifeposition = new Vector3(0.1f, 0.8f, -9.9f); 219 knifeObj.transform.position = new Vector3(knifeposition.x, knifeposition.y, knifeposition.z); 220 //knifeObj.transform.localPosition = Vector3.MoveTowards(knifeObj.transform.localPosition, stop, 10f); 221 is_k_pressed = true; 222 223 } 224 if (is_k_pressed) 225 { 226 227 Vector3 palmpoint = GetPalmPosition(); 228 print(palmpoint); 229 knifeObj.transform.position = Vector3.MoveTowards(knifeObj.transform.position, palmpoint, 0.8f * Time.deltaTime); 230 } 231 if (Input.GetKeyDown(KeyCode.S)) 232 { 233 Application.LoadLevel("test2"); 234 } 235 } 236 237}

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

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

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

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

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

izmktr

2019/07/05 08:34

rightgesture.IsLeft や palm の値を画面に出してみてはどうでしょうか おそらくここのif文で弾かれていると思います
hoheinabachan

2019/07/05 09:58 編集

それぞれDebug.logで出力しましたがNullにはなっていないようでした。 上の方のスクリプトの、Vector3のエラーの数が起動するとどんどん増えていくのですが、それによる処理の遅延かもしれません(先ほどかなりラグがありましたが正常に動いたので)
hoheinabachan

2019/07/08 00:10

失礼致しました、Vector3 pencilposition = bones[3].position;の部分です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問