途中であきらめるかも…
というわけでLepmotionで自分でハンドルを操作するような感じの車を走らせるゲーム?を作ってみたいと思う。
まずはハンドルの傾きで車の走行方向を変えられるようにしたい。
Leapmotionでは手の形を正確にトラッキングできるのだが、流石に見えないところまではトラッキングできないので、手の親指が上向きでグーをすると人差し指や親指が誤反応を起こすので今回は両手の水平方向との傾きを取ってそれのSinでやってみたいと思う。
まずは適当にScene作り。車と地面を用意してハンドルの近くにLeapmotionで手を持っていく。そしてコードを書いてゆく。
ここで、説明したLeap namespaceを利用して、それぞれの手の高さを取ってゆく。コードは以下の通り。
using UnityEngine;
using System.Collections;
using Leap;public class drivehand : MonoBehaviour {
Controller cont;
Hand rightHand;
Hand leftHand;
public float handsAngle;
public float sin;
// Use this for initialization
void Start () {
cont = new Controller();
}
// Update is called once per frame
void Update () {
Frame frame = cont.Frame();
var hands = frame.Hands;
foreach(Hand h in hands)
{
if (h.IsLeft)
leftHand = h;
if (h.IsRight)
rightHand = h;
}
if(leftHand!=null && rightHand != null)
{
checkAngle(rightHand, leftHand);
}
}
void checkAngle(Hand r,Hand l)
{
Vector2 handsVector = new Vector2(r.PalmPosition.x - l.PalmPosition.x, r.PalmPosition.y - l.PalmPosition.y);
float angle = Vector2.Angle(Vector2.right, handsVector);
handsAngle = angle;
sin = Mathf.Sin(Mathf.Deg2Rad*angle);
Debug.Log("digree : " + handsAngle);
Debug.Log("sin : "+sin);
}
}
まだデバック用なのでこれから発展させてゆく。
詰まったところとして、handsVectorと水平なのでVector2.rightとの角度を計算するのだが、それによって出てくるものが度数法なのか弧度法(ラジアン)なのかということがどこにも書いていなかったので書いておくが、これによって出てくるのは度数である。
しかし、Mathfクラスのsinで引数としてあるのは弧度なのである。これによって数時間頭を悩ませることに…。お願いだから公式リファレンスぐらいしっかりして…
次回これで曲がれるようにします。