Tuesday, January 20, 2015

Unity Script: Oculus Selection Logic (2 of 3)

Oculus selection is something I've seen a lot of examples of in demo games, but almost no documentation on as to how to manage your selection. Here's some basic Unity logic I've learned while working on Brune's Oculus integration.
See how we made the pointer in post 1
____________________________________________________________





Goal

Selection: plain and simple. Choosing between game objects, which way to look, and what to interact with. Mainly this post is about how to use a pointer moved by the mouse (last post) that sends clicks where you want them to go.

____________________________________________________________

Obstacles:
Note the curve, and how each eye sees further to one side

Camera Distortion: It's important to keep in mind that the Oculus uses 2 in game cameras distorted together to simulate your eye-based vision inside the Rift. This also means that it's easy to aim at something with "one eye" and not really hit it within the distortion.







Mouse Function: I just haven't managed to get the windows mouse-pointer function in Unity to work inside the Rift. That said, OnMouseDown does work if referenced indirectly such as

public bool triggered;

void OnMouseDown () {
       Application.LoadLevel("MyScene");
}

void Update () {
       if  (triggered) {
          OnMouseDown();
       }
}

I use this to have Oculus and regular mouse function in the same script with the same click-response code.

____________________________________________________________

How to Trigger:
Building text and highlight appear when building is triggered


  1. Make a cube child object of the pointer named "PointCollider" (or whatever)
  2. give it a cube collider set to "Is Trigger" and a rigid body set to "Is Kinematic"
  3. transform it such that it makes a long thin line through pointer object on out into the world as far as you need. (make sure it goes far enough backward to tough the player)
  4. Put a collider and this snip of code on your click-responsive assets:

void OnTriggerEnter(Collider col) {
// this part keeps your script from responding to all collider interactions, only your pointer
       if (col.transform.name == "PointCollider") {
             triggered = true;
      }
}

void OnTriggerStay(Collider col) {
       if (col.transform.name == "PointCollider") {
             triggered = true;
      }
}

void OnTriggerExit(Collider col) {
       if (col.transform.name == "PointCollider") {
             triggered = false;
      }
}


____________________________________________________________

Example In Use

I've done it slightly different in this JS example, using if (Input.GetMouseButtonDown(0)) because OnMouseDown is linked to a GUI confirmation irrelevant to the Oc player.

____________________________________________________________

Raycasts

If you ask around about what to use for generic direction-based selection, much of the Unity community will suggest Raycasting. This is an invisible line you send out from a specified origin along a specified angle. If it hits something, it records what it hit.


Why I don't use them if I can help it:

a) conditionals for raycast hit can only be calculated in the script that sends the raycast. This means that
  • button-like scripts are useless 
  • trigger area scripts are useless
  • you are mostly reliant on tag-management to sort your responses
  • effecting other objects is a logistical pain
b) selecting what the raycast hits is a pain in the butt.
c) programming for Raycasts involves programming with catcher-scripts or backward-pointers that I find not-so-fun.


Why Raycasts are useful anyway and you'll see them later on:


They are the only way (that I know of) to select a point on a TEXTURE and not just a gameObject. Therefore we will be using a Raycast to handle the Web Vewier later.


____________________________________________________________

Wanna see how your Oculus pointer can select specific points on a Coherent UI web view? Tune in to final post of this trilogy next time! :D
____________________________________________________________


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

3 comments:

  1. Hello,

    this is very interesting stuff, please continue :-)

    ReplyDelete
  2. Can i have permission to sure you raycast explanation image? (https://lh6.googleusercontent.com/proxy/UXo-ryYQMpcQeJS3DUYywJEFywxrfZImLFLv-bR4QAWjENTkxlzVPsXC0XDqmS8D9ZeXpqIGf6aV69UzatMd0kB187voQZ4JWqe4ODL_KlvSpfM94kAWb7bOCU3cxUj3Pv_yBw=s0-d) I need it for some university work.

    ReplyDelete