View on GitHub

lectures

Android スマートフォンでの VR(2)

unity/Home


新規シーンを作成する

image

image

プレイヤーキャラクタを置く

image

image

image

image

image

カメラの設定

image

image

デバッグ用のテキスト表示を配置する

image

image

image

C# スクリプトを書く

image

public float moveSpeed = 1.0f;
private Transform headCamera;
private TextMesh debugText;

image

void Start () {
    headCamera = transform.Find("Main Camera");
    if (headCamera == null) {
        Debug.LogError ("Can‘t find Main Camera.");
        return;
    }
    debugText = (TextMesh)headCamera.transform.Find("Debug Text").GetComponent<TextMesh> ();
    if (debugText == null) {
        Debug.LogError ("Can't find Debug Text.");
    }
}

image

void FixedUpdate ()
{
    Vector3 headRot = headCamera.transform.rotation.eulerAngles;
    Vector3 move = new Vector3 (0, 0, 1);
    move = Quaternion.Euler (0, headRot.y, 0) * move;
    move = move * Time.deltaTime * moveSpeed;
    transform.Translate (move, Space.World);
}

image

void Update () {
    Vector3 headRot = headCamera.transform.rotation.eulerAngles;
    debugText.text = string.Format  ("position:({0:F2},{1:F2},{2:F2})\n", transform.position.x, transform.position.y, transform.position.z);
    debugText.text += string.Format("rotation:({0:F2},{1:F2},{2:F2})", headRot.x, headRot.y, headRot.z);
}

image

スクリプトの編集が終わったら、実機にインストールして動作させてみる。
顔を向けている方向に前進するような VR シーンが再生されるはずである。

image

課題


unity/Home