Unity (Rift) Getting Started

The Avatar Unity package contains several prefabs you can drop into your existing Unity projects. This tutorial shows you how to start using them.

Download the Oculus Avatars SDK

The SDK is packaged in a .zip archive file on our developer website.

  1. Download the Oculus Avatars SDK from https://developer.oculus.com/downloads/package/oculus-avatar-sdk/.
  2. Download the Oculus Utilities for Unity 5 from https://developer.oculus.com/downloads/package/oculus-utilities-for-unity-5/.
  3. Extract the contents of the .zip archive files to your local drive.

Set Up the Unity Project for Oculus VR and Avatars

To set up, import the Oculus Unity packages into a project.

  1. Create a New Project in Unity named "Unity Avatar Demo Project".
  2. Import the Oculus Avatars (OvrAvatar.unityPackage) and Oculus Utilities (OculusUtilities.unitypackage) packages. For each package:
    1. Click Assets > Import Package > Custom Package.
    2. Select the package file from your local drive.
    3. Click All and then click Import.
  3. Select the Virtual Reality Supported check box in Edit > Project Settings > Player.
  4. Delete Main Camera from your scene and then drag OVRCameraRig from OVR > PreFabs.
  5. Reset the transform on OVRCameraRig.
Note: You may ignore any No Oculus Rift App ID warnings you see during development. While an App ID is required to retrieve Oculus avatars for specific users, you can prototype and test experiences that make use of Touch and Avatars with just the default blue avatar.

Adding an Avatar to the Scene

The LocalAvatar prefab renders the player's avatar and hands. You can choose which parts of the avatar you want to render: body, hands, and Touch controllers.

To render avatar hands with controllers:

  1. Drag OvrAvatar > Content > Prefabs > LocalAvatar to the Unity Hierarchy window.
  2. In the Unity Inspector window, select the Start With Controllers check box.

Click Play to test. Try out the built-in hand poses and animations by playing with the Touch controllers.

To render avatar hands without controllers:
  1. In the Hierarchy window, select LocalAvatar.
  2. In the Inspector window, clear the Start With Controllers check box.

Click Play to test. Squeeze and release the grips and triggers on the Touch controllers and observe how the finger joints transform to change hand poses. It is possible to add hand poses outside the range of these movements; we talk more about this in Custom Touch Grip Poses.

To render an avatar body:
  1. In the Hierarchy window, select LocalAvatar.
  2. In the Inspector window, select the Show Third Person check box.
  3. Change Transform > Position to X:0 Y:0 Z:1.5
  4. Change Transform > Rotation to X:0 Y:180 Z:0

Recording and Playing Back Avatar Pose Updates

The avatar packet recording system saves avatar movement data as packets you can send across a network to play back on a remote system. Lets take a quick tour of the RemoteLoopbackManager script.

Open the RemoteLoopback scene in OvrAvatar > Samples > RemoteLoopback.

We set RecordPackets to true to start the avatar packet recording system. We also subscribe to the event handler PacketRecorded so that we can trigger other actions each time a packet is recorded.

void Start () {
    LocalAvatar.RecordPackets = true;
    LocalAvatar.PacketRecorded += OnLocalAvatarPacketRecorded;
}

Each time a packet is recorded, our code places the packet into a memory stream we are using as a stand-in for a real network layer.

void OnLocalAvatarPacketRecorded(object sender, args)
{
    using (MemoryStream outputStream = new MemoryStream())
    {
        BinaryWriter writer = new BinaryWriter(outputStream);
        writer.Write(packetSequence++);
        args.Packet.Write(outputStream);
        SendPacketData(outputStream.ToArray());
    }
}

The remainder of our code receives the packet from the memory stream for playback on our loopback avatar object.

void SendPacketData(byte[] data)
{
    ReceivePacketData(data);
}

void ReceivePacketData(byte[] data)
{
    using (MemoryStream inputStream = new MemoryStream(data))
    {
        BinaryReader reader = new BinaryReader(inputStream);
        int sequence = reader.ReadInt32();
        OvrAvatarPacket packet = OvrAvatarPacket.Read(inputStream);
        LoopbackAvatar.GetComponent<OvrAvatarRemoteDriver>().QueuePacket(sequence, packet);
     }
}