Kodeco Forums

HTC Vive Tutorial for Unity

Learn how to use the HTC Vive with Unity! Grab and throw objects, shoot lasers and teleport around an area.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/792-htc-vive-tutorial-for-unity

Something very strange is happening when I play with this. The (right) controller will not show the laser or the teleportReticule however the player will teleport. I wonder if its something to to do with the XYZ rotation of the laser or the reticule.

Hey there,

I’ve been having the same issue. Haven’t been able to figure it out sofar. Also if you update this tutorial you could perhaps mention that when you now create a cube a rigidBody is added by default aswell. I kept thinking why the hell are the boxes moved by my pointer :P…wellllit was the rigidBody.

Found it. I think once the one controller has a reference to the pointer it does something with it that prevents you from just using it for the other one. I fixed it by creating two separate laser “boxes” a left one and a right one and pairing them to the script separately. So, a left laser for the left controller and a right laser for the right. I’m sure there’s a way of doing this in a neater way.

Hi there. This is really a great tutorial! I had a lot of fun and it has been extremly motivating! Thank you very much!

Same trouble getting both controllers work with this teleporting stuff. I solved it the same way as rweijermars - just take one reticle-laser-pair for the left controller and one reticle-laser-pair for the right controller.

Now another problem appears: The laser remains in the air if you move from valid to invalid target while grabbing the touchpad…

@bigdaddio @rweijermars @smift

Hey guys!

I’ll look into those issues as soon as possible. Thanks for creating a workaround Raymond, nice one! :smile:

Cheers!

Thank you so much for this tutorial! It was super illuminating and got me up and running in Unity VR in no time. This is a great starting point for most games.

For those asking about the laser issues, here’s what I adjusted to solve those:

  1. In the editor I made a prefab from the LaserPointer cube and moved both it and the TeleportReticle prefab to a newly created Assets\Resources folder.
  2. In code I Instantiate and Destroy the prefabs as necessary. The easiest way is to change

public Transform laserTransform;
public Transform teleportReticleTransform;

to

public GameObject laser;
public GameObject teleportReticle;

and remove all calls to laserTransform/teleportReticleTransform.gameObject.SetActive(). Then in the if condition of Update() when you would normally update the position you can do a quick

if(!laser && !teleportReticle)
{
laser = Instantiate(Resources.Load(“Laser”)) as GameObject;
teleportReticle = Instantiate(Resources.Load(“TeleportReticle”)) as GameObject;
}

to instantiate both prefabs.
Whenever you need to destroy them (add an else clause for when the RaycastHit check fails and when the button’s not pressed) with

if(laser && teleportReticle)
{
Destroy(laser);
laser = null;
Destroy(teleportReticle);
teleportReticle = null;
}

  1. I also added a quick controller index check to make sure that both controllers can’t be actively trying to teleport at the same time. As a warning this is a mess, I should properly access Valve’s enum instead of casting but it’s late and I’m lazy. Add

public static int buttonHeld = -1;

to the top of your class. If the RaycastHit check is successful, call (the mess)

buttonHeld = (int)trackedObject.index;

and make sure to revert that if the Controller.GetPress() check fails with

buttonHeld = -1;

You’ll also have to add

&& buttonHeld == -1

to the Controller.GetPress() check itself.

As a note Instantiating this way might not be the fastest way to do this–been a few years since I did any serious work in Unity and I don’t know the overhead of the Instantiate() or Resource.Load() calls with a resource name at runtime. I assume it’s low enough that this should work for most things that aren’t absolutely pushing system limits and is on the very low end of optimization strategies, but it’s something I’d have to look into.

Hope this helps!

1 Like

@bigdaddio @rweijermars @trkorecky

Hey guys,

I’ve updated this tutorial with some minor changes so it works flawlessly with 2 lasers (and reticles) without any decrease in performance or increase in complexity.
Thanks for all your helpful input!

Cheers!

Hello Eric,
I am trying to develop a VR game for ps4 on unity 3d. But i have been unsuccessful in trying to map the input of duelshock motion controller in unity 3d. Any idea how could i do that??? Also is it possible to use ps vr headset on unity like htc vive???

@bibeek

Hi!

For controller input mapping, I use InControl: Unity Asset Store - The Best Assets for Game Making , it works great on all kinds of platforms, even mobile and it’s quite easy to get started with. He also has a free version but it’s kinda old so I can’t recommend it.

As for PSVR support, it’s possible, but you’ll need to become a certified Playstation developer and receive a PS4 devkit from Sony. I’m not a PS dev myself so I can’t help you much in that regard.

Cheers!

I sorry,this is out of topic but umm…do you know how can i create player’s Silhouette like in “fitness evolved kinet” ??? i know its related with sharaders bit can’t find anything helpful!!

@bibeek
I’m not sure how to achieve that effect I’m afraid.
You might have more luck by asking this question in the general Unity forum:

Unity - kodeco.com Forums

Cheers!

Thanks for the Tutorial! It’s really easy to follow for a beginner like myself.

I’ve run into a problem with the controllers not showing up…I’ve copied SteamVR and CameraRig into the hierarchy, but when I run the scene the controllers become grayed out and their visibility becomes unchecked. I try to make them visible, but when I start the scene again they still don’t show. I’m confused because that part seems so simple:)

Do you have any idea what could be happening?

Hey @jlum25

That’s a weird problem!
Have you tried updating SteamVR?
Can you restart the headset while the game is running and try again?
Have you checked the cables for any issues?
Are the controllers green in the SteamVR app?
Have you checked for firmware updates?

Sorry about the barrage of questions, but issues like these can be all sorts of things. :smiley:

Cheers!

Hey @blackdragonbe

Thanks for the quick reply!

I tried all of your suggestions, and what I eventually found out is that the version of Unity I was using (5.6) is in Beta, and after downloading most recent stable version, 5.5.2, the problem fixed itself. Didn’t even realize that I had downloaded the beta.

I appreciate the help and thanks again for putting this together.

1 Like

@jlum25

Glad you found a solution! I hope you enjoy(ed) the tutorial.

Cheers!

1 Like

Hi, great tutorial so far, but I have run into what may or may not be a problem towards the start… I get my controllers moving in the environment, I also get the script working (ViveControllerTest) having checked it thoroughly from the final version and yet my Console doesn’t register their input when I play the scene. It just says: “Connected to lighthouse:LHR-362BFA83”

What am I missing here?
(I’ve double checked the process, it all seems like I’ve followed the instructions correctly). Hmm…

Edit- I can pick up and throw objects, yet still no output of data.

Hey @matbrady,

Could you make a new script and put something like:

Debug.Log("Hello World!");

inside the Start() method and attach that to an empty GameObject in the Hierarchy?

That also reminds me, are you sure you added the ViveControllerTest components to the controllers?
This is a common mistake which I often make myself as well so it wouldn’t surprise me. :smile:

Cheers!

Hey Great Work on the vive, I really like your tutorial.

There is a problem with my raycast.

it keeps floating and hanging on air when I move from valid to invalid target while grabbing the touchpad.

I followed your tutorial exactly, it was ok for awhile, after two days of playing with it, the problem start to occurred

[quote=“fgamerz303, post:20, topic:24410”]
I followed your tutorial exactly, it was ok for awhile, after two days of playing with it, the problem start to occurred
[/quote]