Add ray cast from the hand in Unity — Oculus
Welcome to the Oculus Developer Forums!

Your participation on the forum is subject to the Oculus Code of Conduct.

In general, please be respectful and kind. If you violate the Oculus Code of Conduct, your access to the developer forums may be revoked at the discretion of Oculus staff.

Add ray cast from the hand in Unity

Simple question: for Oculus Avatar SDK how do I add the ray coming from the hand ( left or right ) if I take Avatar SDK for Oculus Unity "Controllers" scene, just simple hands.
In order to interact with other objects I would need to add event systems and canvas found examples for those but unable to find a clear explanation how to add this to Avatar hands just want to cast the ray from the hand. I'm using the latest Avatar SDK and the latest Unity 2017.3.f1
 Thank you,
Alex

Best Answer

  • alexvilchalexvilch Posts: 38
    Brain Burst
    Accepted Answer
    Yes, I did. Just add vizualizer object with the ray and it started casting the ray from "active" hand: switching the trigger on the touch controller shifts the ray from one hand to another. None of the functions mentioned above working. You could copy it from this example:


    I could not figure out how to cast the ray from the index finger like in Dash home in my case its coming from the base of the hand: from my understanding this requires making changes to the Animation of the hands.

    Do not bother about those Transform functions and etc. it's simply not working.

    Alex

Answers

  • MikeFMikeF Posts: 674
    Neo
  • alexvilchalexvilch Posts: 38
    Brain Burst
    Thank you, Mike.
    From this doc:

    You can specify the hand and joint you want and then use GetHandTransform() to get its transform.

    public Transform GetHandTransform(HandType hand, HandJoint joint)Is 
    How do I cast the ray from the hand ? Is there any practical example ?
    Alex
  • MikeFMikeF Posts: 674
    Neo
    So what you'l want to do is wait until the avatar is initialized fully, then call Transform raycastOrigin = GetHandTransform(HandType.Left(or right), Handjoint.HandBase).

    Then you can reference that transform.position and transform.forward for any raycasting needs
  • alexvilchalexvilch Posts: 38
    Brain Burst
    Mike,
    Thanks again but I'm new in Avatar/Unity. I wish they would just add a clear example for this one like for grabable objects.
    In Oculus Avatar latest example where it would be the best place to add this code ? Also this would do the raycasting but how I would specify the distance of the ray and its properties ?

     Thank you for your help,

    Alex
  • jimthegrimjimthegrim Posts: 17 Oculus Start Member
    I was just by chance reading this thread a couple of days ago that seems like it might be similar to your problem. Might give it a look: 
    https://forum.unity.com/threads/unity-5-2-vr-raycast-solved.361211/
    (Make sure to read the whole thing as there's a bit of back and forth prior to the OP solving his problem)
    Also (as always...lol) I highly recommend checking out VRTK. Especially if you are new to coding, or just VR dev in Unity in general. :smile:

    VRTK:
    https://vrtoolkit.readme.io
  • Ross_BeefRoss_Beef Posts: 170 Oculus Staff
    edited April 2018
    Hey all, late to the party but had a similar question from another developer and thought this alternative function might be worth sharing. It looks like it's not included in our docs, so will make sure it's added.

    This should be accessible from Unity Avatars integration:
    public void GetPointingDirection(HandType hand, ref Vector3 forward, ref Vector3 up)

    Where forward = finger point and up = thumb point direction
  • alexvilchalexvilch Posts: 38
    Brain Burst
    jimthegreen: According VRTK  in the tutorial for “Getting started with Oculus Avatar” they are currently not supporting Avatar handmodel. So I have to use the controllers from Avatar but not able to use hands just like in Oculus dash home where the Ray is coming out from my hand.
    Ross_Beef: is there any practical example in Unity to add the ray cast coming out from my my hand in Avatar SDK ?

     Thank you all for your help,
    Alex
  • jimthegrimjimthegrim Posts: 17 Oculus Start Member
    edited April 2018
    Ah yes that's correct alexvilch, Apparently it doesn't currently support io for the Oculus hand. my bad. :sweat_smile:
  • MalicoreMalicore Posts: 35 Oculus Start Member
    alexvilch said:
    jimthegreen: According VRTK  in the tutorial for “Getting started with Oculus Avatar” they are currently not supporting Avatar handmodel. So I have to use the controllers from Avatar but not able to use hands just like in Oculus dash home where the Ray is coming out from my hand.
    Ross_Beef: is there any practical example in Unity to add the ray cast coming out from my my hand in Avatar SDK ?

     Thank you all for your help,
    Alex
    I also tried implementing VRTK but decided to switch back due to the lack of handmodel support, and the fact that the developer is slowly walking away from the SDK and plans to release one final update.
  • alexvilchalexvilch Posts: 38
    Brain Burst
    Does anyone know the practical example of how to call GetHandTransform function in Avatar SDK ?

    When I call it I'm getting:

    error CS0246: The type or namespace name `HandType' could not be found. Are you missing an assembly reference?

    Am I supposed to add more of this global include to be able to call it ?

    using UnityEngine;
    using Oculus.Avatar;
    using Oculus.Platform;
    using Oculus.Platform.Models;
    using System.Collections;
     
     Thank you,

    Alex


  • alexvilchalexvilch Posts: 38
    Brain Burst
    I'm trying to call it :   Transform raycastOrigin = GetHandTransform(OvrAvatar.HandType.Left, OvrAvatar.HandJoint.HandBase);

     Getting this:

    error CS1644: Feature `using static' cannot be used because it is not part of the C# 4.0 language specification

    Alex


  • alexvilchalexvilch Posts: 38
    Brain Burst
    Found the definition of this function in LocalAvatar in "Ovr Avatar" script

    public Transform GetHandTransform(HandType hand, HandJoint joint)
        {
            if (hand >= HandType.Max || joint >= HandJoint.Max)
            {
                return null;
            }

            var HandObject = hand == HandType.Left ? HandLeft : HandRight;

            if (HandObject != null)
            {
                var AvatarComponent = HandObject.GetComponent<OvrAvatarComponent>();
                if (AvatarComponent != null && AvatarComponent.RenderParts.Count > 0)
                {
                    var SkinnedMesh = AvatarComponent.RenderParts[0];
                    return SkinnedMesh.transform.Find(HandJoints[(int)hand, (int)joint]);
                }
            }

            return null;
        }

    Alex
  • alexvilchalexvilch Posts: 38
    Brain Burst
    I've figured out the correct syntax to call GetHandTransform:
     Transform raycastOrigin = myAvatar.GetHandTransform(OvrAvatar.HandType.Left, OvrAvatar.HandJoint.HandBase);

    Still no ray appears from Avatar left hand.
    Am I supposed to add Canvas and EventSystem ? What if I just want to cast the ray from Avatar left hand ?
    Still can not figure out how to make it working. I think this AvatarSDK is lacking some simple examples so Developers can start using this.

     Thank you,

    Alex

  • alexvilchalexvilch Posts: 38
    Brain Burst
    I was able to add the laser pointer ( ray cast ) to Avatar SDK. It goes to the left or right hand but not to the "fingers".
    Moreover I do not even have to call this:
    Transform GetHandTransform(HandType hand, HandJoint joint)
    Working without this, I've tried to replace Handjoint.Handbase with any values below, still the ray is coming out from the middle of the hand, not like in Oculus Dash home.
    public enum HandJoint
        {
            HandBase,
            IndexBase,
            IndexTip,
            ThumbBase,
            ThumbTip,
           
            Max,
        }

     Once again, does Avatar SDK Supports adding the raycast attached to the finger not to the entire Handbase ?

     Thank you,

    Alex


  • LightowlLightowl Posts: 17 Oculus Start Member
    Just curious, did you figure this out?
  • LightowlLightowl Posts: 17 Oculus Start Member
    Thanks!
  • vikneshtkvikneshtk Posts: 5
    NerveGear
    Hi
    Reactivating this issue, as I am facing a similar problem to what @alexvilch was facing. I have successfully implemented raycasting from hand but not from fingers. However, the raycasting is not pointing to the forward direction. It always points in one direction. I do not want to use the Oculus raycasting, as I have faced some issues with that. I wanted to create my own raycasting. I have also added linerenderer to show my laser ray. I have given the script below and few screenshots.
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CustomLaserPointer : MonoBehaviour
    {
       public LineRenderer laserLineRenderer;
        public float laserWidth = 0.1f;
        public float laserMaxLength = 5f;
        OvrAvatar ovrAvatar;
    
        //OVRCameraRig cameraRig;
        public Text text;
    
        void Start()
        {
            Vector3[] initLaserPositions = new Vector3[2] { Vector3.zero, Vector3.zero };
            laserLineRenderer.SetPositions(initLaserPositions);
            laserLineRenderer.SetWidth(laserWidth, laserWidth);
            //cameraRig = FindObjectOfType<OVRCameraRig>();
            ovrAvatar = FindObjectOfType<OvrAvatar>();
        }
        void Update()
        {
            if (OVRInput.Get(OVRInput.RawTouch.RIndexTrigger))
            {
                ShootLaserFromTargetPosition(/*cameraRig.rightHandAnchor.localPosition*/
                    ovrAvatar.HandRight.transform.position, Vector3.forward, laserMaxLength);
                text.text = "laser pointer triggered"; //debug text
                laserLineRenderer.enabled = true;
            }
            else
            {
                text.text = "";
                laserLineRenderer.enabled = false;
            }
        }
        void ShootLaserFromTargetPosition(Vector3 targetPosition, Vector3 direction, float length)
        {
            Ray ray = new Ray(targetPosition, direction);
            Vector3 endPosition = targetPosition + (length * direction);
            if (Physics.Raycast(ray, out RaycastHit raycastHit, length))
            {
                endPosition = raycastHit.point;
            }
            laserLineRenderer.SetPosition(0, targetPosition);
            laserLineRenderer.SetPosition(1, endPosition);
        }
    } 
  • vikneshtkvikneshtk Posts: 5
    NerveGear

  • vikneshtkvikneshtk Posts: 5
    NerveGear
    I have tried Hands, Controllers and Hand anchors. Nothing worked as expected. Am I missing anything here?
  • AGTDigitalAGTDigital Posts: 9
    NerveGear
    if (OVRInput.Get(OVRInput.RawTouch.RIndexTrigger))
    Are you sure this condition is passed when you press your button ?

    If not, you should check which controller is connected :
    Debug.Log(OVRInput.GetConnectedControllers());
  • vikneshtkvikneshtk Posts: 5
    NerveGear
    if (OVRInput.Get(OVRInput.RawTouch.RIndexTrigger))
    Are you sure this condition is passed when you press your button ?

    If not, you should check which controller is connected :
    Debug.Log(OVRInput.GetConnectedControllers());
    Yes. The raycast works fine and the line renderer is ON whenever I touch the right trigger. You can see that in screenshots. I have also added a debug text, which assigned to a text object rendered on screen space camera. Only problem I face is the direction of the ray which is not aligned with the controller mesh/object.
  • vikneshtkvikneshtk Posts: 5
    NerveGear
    Finally figured out the laser direction by changing the following line 
    ShootLaserFromTargetPosition(/*cameraRig.rightHandAnchor.localPosition*/
        ovrAvatar.HandRight.transform.position, Vector3.forward, laserMaxLength);
    to
    ShootLaserFromTargetPosition(/*cameraRig.rightHandAnchor.localPosition*/
    ovrAvatar.HandRight.transform.position, ovrAvatar.HandRight.transform.forward, laserMaxLength);
    Hope this will help someone !
  • lostJamlostJam Posts: 1
    NerveGear
    this was so helpful wow thank you!!!! T^T
Sign In or Register to comment.