Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nmgh101 · May 17, 2016 at 09:51 PM · movementvrmovement script

Traditional movement in vr using the vive controllers?

So I am trying to make a game in which the player moves using the trackpad on one of the HTC Vive controllers. Right now I have a script that can move the camerarig with the trackpad, but it will only move in the direction the camerarig is facing when it starts (so if the player looks left and presses forward on the trackpad, in their pov they will be moving to the right.) How can I get this working so that the camerarig moves in the direction the player is facing?

 using UnityEngine;
 using System.Collections;
 using Valve.VR;
 
 public class touchPad : MonoBehaviour
 {
     public GameObject player;
     //player is the camerarig
     SteamVR_Controller.Device device;
     SteamVR_TrackedObject controller;
 
     Vector2 touchpad;
 
     void Start()
     {
         controller = gameObject.GetComponent<SteamVR_TrackedObject>();
     }
 
     // Update is called once per frame
     void Update()
     {
         device = SteamVR_Controller.Input((int)controller.index);
         //If finger is on touchpad
         if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
         {
             //Read the touchpad values
             touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
 
             player.transform.position = new Vector3(player.transform.position.x + (-touchpad.y/100f), player.transform.position.y, player.transform.position.z + (touchpad.x/100f));
         }
     }
 }

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mmmpies · May 23, 2016 at 01:43 PM

It's tricky @nmgh101 because the head look isn't the rotation of the Camera, forward is still forward for the Vive setup no matter where your head's looking and if you rotate the parent position to be the direction your head's facing then you'll just end in a flat spin.

I've setup a controller (right or left - whichever your preference is) with this script...

 using UnityEngine;
 using System.Collections;
 using Valve.VR;
 
 public class myTouchpad : MonoBehaviour
 {
     public GameObject player;
 
     SteamVR_Controller.Device device;
     SteamVR_TrackedObject controller;
 
     Vector2 touchpad;
 
     private float sensitivityX = 1.5F;
     private Vector3 playerPos;
 
     void Start()
     {
         controller = gameObject.GetComponent<SteamVR_TrackedObject>();
     }
 
     // Update is called once per frame
     void Update()
     {
         device = SteamVR_Controller.Input((int)controller.index);
         //If finger is on touchpad
         if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
         {
             //Read the touchpad values
             touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
 
 
             // Handle movement via touchpad
             if (touchpad.y > 0.2f || touchpad.y < -0.2f) {
                 // Move Forward
                 player.transform.position -= player.transform.forward * Time.deltaTime * (touchpad.y * 5f);
 
                 // Adjust height to terrain height at player positin
                 playerPos = player.transform.position;
                 playerPos.y = Terrain.activeTerrain.SampleHeight (player.transform.position);
                 player.transform.position = playerPos;
             }
 
             // handle rotation via touchpad
             if (touchpad.x > 0.3f || touchpad.x < -0.3f) {
                 player.transform.Rotate (0, touchpad.x * sensitivityX, 0);
             }
 
             //Debug.Log ("Touchpad X = " + touchpad.x + " : Touchpad Y = " + touchpad.y);
         }
     }
 }

Have a parent to the camera, put a rigidbody with Y constrained on both Rotation and Position. Put that script on your controller and drag the camera Parent object onto the public player object.

It's a bit of a combination of a traditional controller WASD/mouse, forward backward and turn whilst your head can still look around.

Really I'd add an option to teleport as well as some people will feel ill with this approach. I guess I'm not one of them given how much time I spent spinning around on the floor trying to get the script to work! :)

Oh and if you get a steep slope it'll fall apart trying to set the height so lock off anything too steep.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bnielson · Mar 04, 2017 at 10:16 PM 0
Share

How do you "lock off" terrain in Unity3d?

avatar image
0

Answer by ProtoPottyGames · May 17, 2016 at 11:17 PM

You'll have to parent it to another object and move the parent object instead. The steamvr camerarig will only face the direction of it's original orientation.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nmgh101 · May 17, 2016 at 11:37 PM 0
Share

How would I need to change the parent object's orientation?

avatar image ProtoPottyGames nmgh101 · May 18, 2016 at 12:03 AM 0
Share

The parent's orientation can be changed in the same way that you would normally change it with a "regular" controller. You just need to change the controller input information in your script so it uses the vive controller's inputs ins$$anonymous$$d. Try sticking the camerarig on one of the vehicle prefabs in the standard assets folder (as a child) and test it out with an Xbox 360 controller (or whatever you usually use.) Then you can just swap out your original inputs with the S$$anonymous$$mVR ones.

avatar image ProtoPottyGames nmgh101 · May 18, 2016 at 02:50 AM 0
Share

Here's a script I found on git. There's probably a better one out there to reference somewhere, but this one looks to have most, if not all of the vive controller's button information. (Haven't had much time to do a really good look over it though, sorry.) I'll have to double check it later tonight to confirm.

https://github.com/ValveSoftware/openvr/blob/master/unity_package/Assets/S$$anonymous$$mVR/Scripts/S$$anonymous$$mVR_TestController.cs

avatar image ProtoPottyGames nmgh101 · May 18, 2016 at 02:59 AM 0
Share

Ha! $$anonymous$$y bad, scratch that.. Here's your pony. ;)

https://github.com/ValveSoftware/openvr/wiki/VREvent_t

avatar image ProtoPottyGames · May 18, 2016 at 06:03 PM 0
Share

Ha! Double scratch that for anyone still needing anything whatsoever regarding vive input information. Do this ins$$anonymous$$d..

Step 1.) Watch this guy's brilliant (ADD friendly) videos:

Theston $$anonymous$$ Fox - YouTube https://m.youtube.com/watch?v=hr5OoSCksnY

Step 2.) Use that guy's amazing scripts.

Step 3.) Live the dream. ;D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement input problem? 1 Answer

Character Movement on Cylinder Surface 0 Answers

VR Vive Tracker barely moving 1 Answer

Space Shooter Movement Scrip not working.,Space Shooter Tutorial Moving The player Errors in parts of code containing Rigidbody.(Insert Velocity, Position, Rotation) 0 Answers

Isometric movement for MoveTowards 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges