Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 embodied_computation · Jul 17, 2018 at 03:21 PM · vrmagnitudehaptic

How can I increase a value while another value decreases: inverse correlation during Update?

I am building an interactive VR experience using the HTC Vive and the SteamVR Unity plugin. My experience involves navigating a space filled with "Sound Spheres" that generate sound when colliding with the Vive controllers. These Sound Spheres are static i.e they sit still in space. I am attempting to write code that helps users locate the spheres using haptic feedback. Currently, I have simple code that measures the distance between a controller and each Sound Sphere. Once the controller is close to the Sound Sphere - haptics fire at a constant intensity.


However, I would like the haptics to increase in intensity as the controller approaches the Sound Sphere. In other words, the intensity should increase as the distance between the controller and the Sound Sphere decreases. I am unsure how to increase one value while the other decreases on update. Please advise. Functional code is below:

 public class ControllerHaptics : MonoBehaviour {
 
     SteamVR_TrackedObject trackedObject;
     SteamVR_Controller.Device device;
 
     GameObject[] soundSpheres;
 
     // Use this for initialization
     void Start()
     {
         trackedObject = GetComponent<SteamVR_TrackedObject>();
         device = SteamVR_Controller.Input((int)trackedObject.index);
 
         soundSpheres = GameObject.FindGameObjectsWithTag("Grabbable");
     }
 
     //Update is called once per frame
     void Update()
     {
         FireHapticsBasedOnDistance();
     }
 
     private void FireHapticsBasedOnDistance()
     {
         foreach (GameObject soundSp in soundSpheres)
         {
             // controller approaches Sound Sphere
             if ((transform.position - soundSp.transform.position).magnitude < 0.5f) //if the distance between the controller and the Sound Sphere is less than 0.5 then:
             {
                 //we have found a Sound Sphere close to a controller
                 device.TriggerHapticPulse(600); // <------- this value should increase as the above magnitude decreases. note that this value is a "ushort" value and probably needs to be converted? 
 
                 break;
             }
         }
     }
 
 


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
0

Answer by Hoax1 · Jul 19, 2018 at 03:43 PM

I would have a float thats called originalValue for example and set that to the value you want to change in start. Then you set your value that you want to change in update to originalValue / distance.

Edit: I don't know if I understood the question correctly or not but I hope this helps :D

Comment
Add comment · 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
0

Answer by mikewarren · Jul 19, 2018 at 07:15 PM

You just need to create a ratio that maps the min/max distance range to the min/max haptic pulse range. Something like below. (You'll need to adjust units and range values.)

     float minDist = 0;  // meters
     float maxDist = 1;  // meters
     float minDistHaptic = 1000;  // Haptic units
     float maxDistHaptic = 0; // Haptic units
     
     float dist = <distance between objects>
     float hapticValue = minDistHaptic + ( dist - minDist ) / ( maxDist - minDist ) * (maxDistHaptic - minDistHaptic);
     hapticValue = Mathf.Clamp( hapticValue, maxDistHaptic, minDistHaptic );
 
 

Comment
Add comment · 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

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

175 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

Related Questions

How to change the small circle in google cardboard with another texture? 0 Answers

How to make a proper Drag&Drop (any GameObject) for the Hololens (Unity3D) 0 Answers

load scene after video ended 2 Answers

VR Cardboard SDK 1.120 and Unity 2017.3 : How to detect magnet trigger ???? 0 Answers

Play 360 Video in Unity 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