Ads Top

How to create and run a macOS VR app using Unity: Part 2

Ever want to create a simple VR application to run on your HTC vive on macOS? Here's how!

If you've been eying the latest trends in game development you couldn't possibly miss the recent hype (deserved in my opinion) around VR and AR applications. We've also spoken at length about installing and running an HTC Vive VR headset on your Mac running macOS High Sierra.

With an eGPU development kit offered by Apple or with the newest iMac Pro running a powerful AMD Radeon Vega GPU, you can create and then run VR applications directly on a connected HTC Vive VR headset. Here's part2 on how to create and run a simple VR application on macOS and HTC Vive! Take a look at part 1 to get you up to speed!

This tutorial was written following the video tutorial provided by VRGameDev so be certain to check out their channel!

How to make your hands

To keep things simple, the "hands" in our application will be two simple spheres but will be able to use the trigger buttons on the HTC Vive controller to grab the cube.

  1. Under Hierarchy again, select Create > 3D object > Sphere.
  2. Select Sphere.
  3. Under Transform, change the scale to X=0.1, Y=0.1, and Z=0.1.
  4. Leave the position at X=0, Y=0, and Z=0.
  5. Select Sphere under Hierarchy and rename it to LeftHand.
  6. Option-Click LeftHand and and select Duplicate.
  7. Rename the duplicate to **RightHand.
  8. Select both LeftHand and RightHand.

  9. Under Sphere Collider click the gear and Remove Component (we don't want our hands to collide with each other in VR).

How to copy some C# code

Since learning C# or other programming languages is beyond the scope of this article, we can import code to tell our application what to do with the input controllers of the HTC Vive. You can copy the code here into a simple text editor (I use vi), save it as HandGabbing.cs, and finally to merge it into your VR application.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR; //needs to be UnityEngine.VR in version before 2017.2

public class HandGrabbing : MonoBehaviour {

public string InputName;
public XRNode NodeType;
public Vector3 ObjectGrabOffset;
public float GrabDistance = 0.1f;
public string GrabTag = "Grab";
public float ThrowMultiplier=1.5f;

private Transform _currentObject;
private Vector3 _lastFramePosition;

// Use this for initialization
void Start()
{
    _currentObject = null;
    _lastFramePosition = transform.position;

}

// Update is called once per frame
void Update()
{
    //update hand position and rotation
    transform.localPosition = InputTracking.GetLocalPosition(NodeType);
    transform.localRotation = InputTracking.GetLocalRotation(NodeType);


    //if we don't have an active object in hand, look if there is one in proximity
    if (_currentObject == null)
    {
        //check for colliders in proximity
        Collider[] colliders = Physics.OverlapSphere(transform.position, GrabDistance);
        if (colliders.Length > 0)
        {
            //if there are colliders, take the first one if we press the grab button and it has the tag for grabbing
            if (Input.GetAxis(InputName) >= 0.01f && colliders[0].transform.CompareTag(GrabTag))
            {
                //set current object to the object we have picked up
                _currentObject = colliders[0].transform;

                //if there is no rigidbody to the grabbed object attached, add one
                if(_currentObject.GetComponent<Rigidbody>() == null)
                {
                    _currentObject.gameObject.AddComponent<Rigidbody>();
                }

                //set grab object to kinematic (disable physics)
                _currentObject.GetComponent<Rigidbody>().isKinematic = true;


            }
        }
    }
    else
    //we have object in hand, update its position with the current hand position (+defined offset from it)
    {
        _currentObject.position = transform.position + ObjectGrabOffset;

        //if we we release grab button, release current object
        if (Input.GetAxis(InputName) < 0.01f)
        {
            //set grab object to non-kinematic (enable physics)
            Rigidbody _objectRGB = _currentObject.GetComponent<Rigidbody>();
            _objectRGB.isKinematic = false;

            //calculate the hand's current velocity
            Vector3 CurrentVelocity = (transform.position - _lastFramePosition) / Time.deltaTime;

            //set the grabbed object's velocity to the current velocity of the hand
            _objectRGB.velocity = CurrentVelocity * ThrowMultiplier;

            //release the reference
            _currentObject = null;
        }

    }

    //save the current position for calculation of velocity in next frame
    _lastFramePosition = transform.position;


}

}

How to correlate you hands with an input device

  1. On the top menu bar click Edit > Project Settings > Input.
  2. Under InputManager Option-Click Horizontal.
  3. Select Duplicate Array Element.
  4. Rename the new element to TiggerLeft.
  5. Set Type to Joystick Axis.
  6. Set the Axis to 11th axis (Joysticks).
  7. Option-Click TriggerLeft.
  8. Select Duplicate Array Element.
  9. Rename the new element to TiggerRight.
  10. Set the Axis to 12th axis (Joysticks).
  11. Option-Click the blank space under Assets.
  12. Select Import New Asset.
  13. Select the HandGrabbing.cs file you downloaded previously.
  14. Click Import.
  15. Select both LeftHand and RightHand under Hierarchy.
  16. Drag and drop the HandGrabbing.cs script onto the Inspector window.

  17. Select LeftHand under Hierarchy and change the input name to TriggerLeft.
  18. Select Left Hand under Node Type.
  19. Select RightHand under Hierarchy and change the input name to TriggerRight.
  20. Select Right Hand under Node Type.

How to make the cube interactive

Finally, let's apply interaction on the cube.

  1. Select the Cube under Hierarchy.
  2. Under Inspector click the drop-down next to Tag.
  3. Select Add Tag.
  4. Click the +.
  5. Name the Tag Grab.
  6. Click Cube avian under Hierarchy.
  7. Click Tag.
  8. Select Grab.

How to build and play your VR App

Once everything is inlace, you can press the Play button and run your VR App in realtime.

  1. Press the Play triangle.
  2. Select a screen resolution that will run on the HTC Vive (Such as 1650 x 1050).
  3. Click Play!.

Your SteamVR application should start and you should now be able to use your application in VR. You can grab your Cube and trow it some distance! Congratulations! You've created your first VR application!.

Final thoughts

This is merely a taste of how to create a VR application. Hopefully you'll be tempted to make it a full meal. Game engines like Unity and Unreal make it very approachable for new aspiring game developers to get their feet wet and start creating amazing VR games. So what are you waiting for?! Tell us what you'll be developing in VR on macOS?



from iMore - Learn more. Be more. http://bit.ly/2yMqQkr
Powered by IOSCUBE

No comments:

Powered by Blogger.