Saturday, January 17, 2015

Unity Script: Oculus 3d Pointer (1 of 3)



One of the biggest obstacles to developing for the Oculus is simulating mouse. This post will take us through the first of three basic steps to get Oculus mouse integration working. We'll create a 3d mouse-object that follows the mouse-movement. Next time we'll add click-integration using triggers in post 2 and we'll finish with click-integration for Coherent UI web views.

____________________________________________________________

Myriad Limitations:

There are a couple of very good reasons why we can't just use the mouse the normal Unity way.
  1. Oculus won't display the windows mouse-cursor
  2. You can't hijack/alter the windows mouse-cursor at-all easily
  3. The windows mouse-cursor aims wrong through the Oculus distortion -even if you can see it-
  4. Oculus ALSO doesn't display Unity GUI, which only responds to windows-mouse anyway. (suggest 3d menu objects as well)

____________________________________________________________

The Solution:


A Unity-Object 3D Pointer

That's right. We're gonna take a Unity asset and turn it into our pointer with a few simple scripting tricks. We'll start with your player-camera and your pointer. I use a red sphere-object as default but you can use whatever works for you.


  • Create your object
  • Parent object under Right Eye Anchor
    •  this means it'll stick to the front of the head when you move
  • Transform thusly
  • It should look something like this with the pointer selected

  • Finally, add this script that will link the pointer-movement to your physical mouse movement. 
    • This responds like MouseLook and doesn't need the windows-mouse

____________________________________________________________

The Script



For Copying


using UnityEngine;
using System.Collections;

public class Pointer_Mock : MonoBehaviour {

public GameObject targetCam;
// Use this for initialization
void Start () {
targetCam = transform.parent.gameObject;
}

void Update() {
transform.LookAt (targetCam.transform);

float localX = transform.localPosition.x;
float localY = transform.localPosition.y;

float h = (localX += (Input.GetAxis("Mouse X") * 0.05f));
float v = (localY += (Input.GetAxis("Mouse Y") * 0.05f));
transform.localPosition = new Vector3 (h, v, transform.localPosition.z);

}
}

____________________________________________________________


All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Download the current Unity client On IndieDB

1 comment: