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 /
avatar image
0
Question by Scymantic · Jun 19, 2018 at 09:19 PM · c#movementunity5directionmotion-tracking

Tracking HTC Vive Controller Motion Direction?

Hi all! I was hoping I could get some guidance on how to accurately track HTC Vive controller directional (up,left,right,down) movement.

The problem: I'm trying to do some motion activated controls. Pretty simple, the user holds down the trigger and if he or she moves the controller to the right, a UI slider increases in value. If he or she moves the controller to the left, the slider decreases in value. However, the increase and decrease directions are only valid, if I'm facing a certain direction (let's call it forward or 0 degrees). If I instead face "backwards" (flip around 180 degrees), when the controller is moved to the left, the values increase, and they decrease when the controller is moved to the right (i.e. the everything flips). If i stand at either 90 or 270 degrees, functionality breaks down completely.

What I suspect: When the user spins around, the x-plane does not move with them.

What I Have Done:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(SteamVR_TrackedController))]
 public class VRSliderControl : MonoBehaviour {
 
     public Slider slider;
     public GameObject label;
     public Image knob;
 
     // Changing this value will change by how much the slider value is incremented
     public float incrementBy = 0.1f;
 
     private SteamVR_TrackedController device;
     private float oldDevicePosX;
 
     // Changing this value will cause the slider knob to "move faster"
     private float posIncrement = 0.01f;
 
    
     private float thresholdValue = 0.01f;
 
 
     // Use this for initialization
     void Start ()
     {
         PlayerPrefs.SetFloat("MoveSpeed", 0f);
 
         // Capture Trigger Held Event
         oldDevicePosX = transform.position.x;
         device = GetComponent<SteamVR_TrackedController>();
 
        
         device.TriggerHeld += OnTrigger;
     }
     
     void OnTrigger(object sender, ClickedEventArgs e)
     {
         
         
         // Increase slider value if user moves controller to the right
         if(transform.position.x > oldDevicePosX && Mathf.Abs(transform.position.x - oldDevicePosX) > thresholdValue)
         {
             oldDevicePosX = transform.position.x;
             // time.deltatime here smooths the slider handle movement
             knob.transform.position = new Vector3(knob.transform.position.x + posIncrement * Time.deltaTime, 
                                                   knob.transform.position.y, 
                                                   knob.transform.position.z);
             slider.value += incrementBy;
             SteamVR_Controller.Input((int)device.controllerIndex).TriggerHapticPulse(1000);
         }
         // Decrease slider values if user moves controller to the left
         else if (transform.position.x < oldDevicePosX && Mathf.Abs(transform.position.x - oldDevicePosX) > thresholdValue)
         {
             oldDevicePosX = transform.position.x;
             knob.transform.position = new Vector3(knob.transform.position.x - posIncrement * Time.deltaTime, 
                                                   knob.transform.position.y, 
                                                   knob.transform.position.z);
             slider.value -= incrementBy;
             SteamVR_Controller.Input((int)device.controllerIndex).TriggerHapticPulse(1000);
         }
 
         // Update the slider label so that the user knows what the slider value is
         label.GetComponent<LabelSettings>().SetLabelText(slider.value.ToString("F1"));
         PlayerPrefs.SetFloat("MoveSpeed", slider.value);
 
     }
 }
 

The Question: Is there a way to get the X-plane to move with the user so that their look direction is always the forward direction / is there just a better more robust way of doing something like this?

Thanks for the help! It's much appreciated!

Comment
Add comment · Show 2
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 Scymantic · Jun 20, 2018 at 05:52 AM 0
Share

@andrew-lukasik Thanks for the reply! I’ll definitely try that tomorrow when I’m back at work and let you know if it did the trick!

avatar image Scymantic Scymantic · Jun 20, 2018 at 04:44 PM 0
Share

@andrew-lukasik After some testing you are absolutely correct and Vector projections are the way forwards however I'm still getting some undesirable behavior. First though here is what the code looks like with the addition of projections:

 void OnTrigger(object sender, ClickedEventArgs e)
 {
     var camera_right = playerCamera.transform.right;
     var camer_pos = playerCamera.transform.position;
     
     // Transform.position = controller position
     var temp_vec1 = transform.position - camera_pos;
     var temp_vec2 = Vector3.Project(temp_vec1, camera_right);
 
     var projected_pos = camera_pos + temp_vec2;
 
     // This structure for projecting vectors is taken from https://answers.unity.com/questions/598060/vector3project-how-to.html



    if (projected_pos.x > oldProjectedX && $$anonymous$$athf.Abs(projected_pos - oldProjectedX) > threshold)
    {
         oldProjectionX = projected_pos.x;
         
         // incresae knob position by posIncrement (same as before)
        // change slider values, trigger haptic pulse etc.
     }
 
   // same thing for if the value is less than except decrease slider values
 }


This works alright in the "forward" direction and the "right" direction (relative to forward) however the axis is still flipped when facing the "left" or "backwards" directions. In both the "left" and "right" directions, a threshold value of 0.001 is too high (the x -position-value change in these directions is extremely small) which leads to a very slow slider response however in the "forwards" and "backwards" directions the threshold value is too low (the x-position-value change in these directions is much larger) leading to a slider that is too responsive and increments by too much on the slightest movement.

Any ideas on reversing the axis for the "backwards" and "left" directions so that moving the controller right still increases the values rather than decreasing them? (I was thinking I could get the angle between the camera and controllers and if it is greater than 120, flip the inequality this is rather crude but seems to be fairly effective. However, the angle for the "left" direction can be anywhere between 120 and 40 degrees making it hard to accurately flip the inequalities) Or fixing the sensitivity of the slider when facing "left" and "right"? ($$anonymous$$aybe I should project onto a different axis?, not having a threshold at all is a bad idea as then it is nearly impossible to control the slider accurately).

Thanks again, and let me know if you need more info.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sgthale · Jun 20, 2018 at 11:21 PM

Your UI virtual screen I assume is what the motion should be relative to correct? What you need then is Transform.InverseTransformDirection( yourControllersVelocity );

This turns your controller's velocity to the screen's local coordinates. So no matter what orientation the screen is, it will always have the same relative coordinates on your controller. I.e. move along the screen increases the x, move up along the screen increases the y, etc.

Comment
Add comment · Show 2 · 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 Scymantic · Jun 21, 2018 at 06:08 AM 0
Share

Hey! Thanks for the reply this sounds exactly like what I need what I thought could be done! The rotation is relative to the player's current look direction in other words, the position of the H$$anonymous$$D (I assume this is what you mean by UI Virtual screen? or do you mean the world space UI were the slider exists? Regardless position is also relative to the latter since the UI is tied to the camera view anyway). Also, the s$$anonymous$$mVR controllers don't have a rigidbody (easy enough to change of course) I just want to make sure that by velocity you mean the rigidbody property :). Unfortunately I'm out of town and I won't be able to apply this until I get back but I definitely will and let you know if it did the trick!

avatar image Scymantic · Jul 04, 2018 at 05:31 PM 0
Share

To anyone interested, here is resultant code:

alt text

The motion is quite fast and I have yet to figure out a good solution to slow it down a bit but it is usable and more importantly acts the same regardless of player look direction. Thanks again @sgthale for pointing me in the direction of transform.InverseTransformDirection()

capturecode.png (39.7 kB)

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

531 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 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 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 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 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 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 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

Making a bubble level (not a game but work tool) 1 Answer

Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer

Moving in the direction of mouse 3 Answers

Distribute terrain in zones 3 Answers

Moving object along with raycast 1 Answer


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