• 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
Question by HelloPeopIe · Jan 22, 2020 at 04:46 PM · vrdistancesteamgrab2.0

SteamVR 2.0 how to implement distance grab?

I am trying to create my own scripts called DistanceGrabber for my hands and DistanceGrabbable for the objects I want to be able to grab from a distance. what I made kind of works but the laser pointer script is making me have to release the grip button first before executing the onDistanceGrab function in DistanceGrabbable script, so I had to "double click" rapidly and hold the grip the second time to be able to hold the object in the hand or else it will just fall to the ground.

and also

how do you disable the line when the laser is not pointing at a grabbable object?


My setup:

Unity 2019.2.6f1

SteamVR 2.0

Oculus Rift


DistanceGrabber script:

 public class DistanceGrabber : MonoBehaviour
 {
     private SteamVR_LaserPointer laserPointer;
     private Hand hand;
 
     void Awake()
     {
         laserPointer = GetComponent<SteamVR_LaserPointer>();
         hand = GetComponent<Hand>();
 
         laserPointer.PointerIn += PointerInside;
         laserPointer.PointerOut += PointerOutside;
         laserPointer.PointerClick += PointerClick;
 
         //interactable = GetComponent<Interactable>();
     }
 
     public void PointerClick(object sender, PointerEventArgs e)
     {
         if (e.target.GetComponent<DistanceGrabbable>() != null)
         {
             e.target.GetComponent<DistanceGrabbable>().onDistanceGrab(hand);
         }
     }
 
     public void PointerInside(object sender, PointerEventArgs e)
     {
         if (e.target.GetComponent<Interactable>() != null)
         {
             hand.ShowGrabHint();
             hand.IsStillHovering(e.target.GetComponent<Interactable>());
             laserPointer.active = true;
         }
     }
 
     public void PointerOutside(object sender, PointerEventArgs e)
     {
         if (e.target.GetComponent<Interactable>() == null)
         {
             hand.HideGrabHint();
             laserPointer.active = false;
         }
     }



DistanceGrabbable script:

 public class DistanceGrabbable : MonoBehaviour
 {
     protected Rigidbody rb;
     protected bool originalKinematicState;
     protected Transform originalParent;
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody>();
         originalParent = transform.parent;
 
         originalKinematicState = rb.isKinematic;
     }
 
     public void onDistanceGrab(Hand hand)
     {
         rb.isKinematic = true;
         transform.SetParent(hand.gameObject.transform);
         hand.AttachObject(gameObject, GrabTypes.Trigger);
     }
 
     private void OnDetachedFromHand(Hand hand)
     {
         rb.isKinematic = false;
         transform.SetParent(originalParent);
     }
 }
Comment
rgbeans
Vaupell
Soareverix

People who like this

3 Show 0
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

Answer by rgbeans · Apr 05, 2020 at 12:43 PM

@HelloPeopIe I have a solution to your problem. My code doesn't use a visible laser pointer, but instead, the objects I am pointing at have a c$$anonymous$$ld object with a SteamVR hover $$anonymous$$ghlight material, as shown in the screenshot below. alt text

but once you have that cube as a c$$anonymous$$ld of your base object, here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Valve.VR.InteractionSystem;
 using Valve.VR;
 
 public class DistanceGrab : MonoBehaviour
 {
     public Transform pointer;//the transform the laser starts at
     public LayerMask t$$anonymous$$ngsWeCanGrab;//t$$anonymous$$ngs we can grab
 
     Hand hand;//our hand
     bool isAttached = false;//do we have somet$$anonymous$$ng in our hand?
     GameObject attachedObject = null;//what do we have in our hand
     Blank blank;//blank script
 
     // Start is called before the first frame update
     void Start()
     {
         hand = GetComponent<Hand>();//get our hand
     }
     // Update is called once per frame
     void Update()
     {
         //raycast and check if our hand is empty
         RaycastHit $$anonymous$$t;
         if (Physics.Raycast(pointer.position, pointer.forward, out $$anonymous$$t, 10f, t$$anonymous$$ngsWeCanGrab) && hand.currentAttachedObject == null)
         {
             Interactable interactable = $$anonymous$$t.collider.gameObject.GetComponent<Interactable>();
             SteamVR_Input_Sources source = hand.handType;
             //are we pressing grip and trigger?
             if (hand.grabGripAction[source].state == true && hand.grabPinchAction[source].state == true)
             {
                 //does the interactable component exist?
                 if (interactable != null)
                 {
                     //move the object to your hand
                     interactable.transform.LookAt(transform);
                     interactable.gameObject.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 500, ForceMode.Force);
                     attachedObject = interactable.gameObject;
                     isAttached = true;
                     //attac$$anonymous$$ng to hand is in the late update function
                 }
             }
             blank = $$anonymous$$t.collider.gameObject.GetComponentInC$$anonymous$$ldren<Blank>();
             blank.gameObject.GetComponent<MeshRenderer>().enabled = true;
         }
         else
         {
             blank.gameObject.GetComponent<MeshRenderer>().enabled = false;
         }
     }
     private void LateUpdate()
     {
         //did we get an object to our hand during t$$anonymous$$s update?
         if (isAttached)
         {
             //attach the object
             hand.AttachObject(attachedObject, GrabTypes.Grip);
             attachedObject = null;
             isAttached = false;
         }
     }
 }
 


annotation-2020-04-05-084112.png (360.9 kB)
Comment
Vaupell
Soareverix

People who like this

2 Show 4 · 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 Vaupell · Apr 10, 2020 at 06:45 PM 0
Share

this

Blank blank;

How in the he.... this you get from somewhere, because by default i cant use that in these namespaces..

avatar image Vaupell Vaupell · Apr 10, 2020 at 07:37 PM 0
Share

Never mind, a blank script, mean a blank script.. haha..

This is great, it works perfect for SteamVR 2.0 distance grab.. I raise my glass to You sir ..

avatar image Giantbean · May 04, 2022 at 01:27 PM 0
Share

@rgbeans Where do you put this script? Trying it on the hand, the object being grabbed or on an empty game object all failed and continually threw errors about a Null Reference Exception at line 50 and 27?

avatar image Giantbean Giantbean · May 05, 2022 at 02:22 PM 0
Share

Neverminded I found another system using a sphere raycast that works. It has a few issues to work out but its not throwing errors. http://egorfesenko.com/vrdistancegrab

avatar image

Answer by jjn7 · Apr 05, 2020 at 11:29 PM

@HelloPeopIe Hey man your code helped me earlier when I tried to implement the same t$$anonymous$$ng. However after testing your code I found that the laser pointer had a lot of issues and I eventually came up with the same conclusion as rgbeans that a Raycast $$anonymous$$t would work better.

However I'm assuming you have included the SteamVR_LaserPointer.cs as a laser to guide the user. Unfortunately turning the script on or off is a hassle and doesn't really work: https://answers.unity.com/questions/1455089/vive-laser-pointer-turn-off.html https://answers.unity.com/questions/1629859/how-can-i-disable-the-steamvr-laser-how-can-i-disa.html

The solution around t$$anonymous$$s would be to change the t$$anonymous$$ckness of the laser when the raycast $$anonymous$$ts the specific object. For example:

 if (Physics.Raycast() $$anonymous$$ts the object) or public void PointerInside(object sender, PointerEventArgs e)
 {
     laserPointer.t$$anonymous$$ckness = 0.002f;
 }
 else
 {
     laserPointer.t$$anonymous$$ckness = 0;
 }

If you need anymore help hmu.

Comment
Vaupell

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

229 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

Related Questions

Spawn object in hand and bind it to hand transform? 2 Answers

SteamVR 2.0 how to get trackpad input 1 Answer

[SOLVED] - Unity VR - I want that the Object knows when it is being grabbed 1 Answer

Object is loosing its texture after pick up with the vive controller 0 Answers

Drag UI Slider in VR with Vive Controllers 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