/ Forums
New To The Forum? Click Here To Read The How To Guide. -- Developers Click Here.

Avatar Grab sample available (Unity)

mfmfmfmf Posts: 47 Oculus Staff
Hey folks: we've put together a sample of using the Avatar SDK with a few scripts to add object grab/throw functionality. I've attached it here. It requires the Oculus Utilities for Unity, and the Avatar SDK. 

This isn't the end of our hand samples, but I didn't want to leave you without at least a basic grab sample for too long. 

Known issue: it works in 5.5.0f3, but crashes in all 5.4.x versions of Unity I've tested so far. For now it's a 5.5-only sample. It doesn't rely on any 5.5-only functionality, however, so you should be able to use the scripts in 5.4 without a problem.

EDIT: updated to behave well with locomotion such as OVRPlayerController, 1/3/2017.
«1

Comments

  • KevinLongtimeKevinLongtime Posts: 282
    Nexus 6
    Thank you!!!!
  • KevinLongtimeKevinLongtime Posts: 282
    Nexus 6
    edited December 2016
    I can't for the life of me figure out how to simply have an avatar that grabs and can walk around. The example given shows a static camera. Seems it should be as easy as switching out the static camera for a OVR player prefab but that's not working for me currently...the hands always end up way in the wrong spot.
  • DorakoDorako Posts: 4
    Virtual Boy (or Girl)
    Thank you for this sample. Any ETA or timeline on other samples?
    At the moment the toys in the test scene has their physics interpolation set to 'none'. This means that they are visibly juddery, especially when they are in your hands. You can fix this by setting the interpolation setting to 'extrapolate', but currently this causes objects to quickly lose their position relative to the hands. What gives? My own grabbing project does not have this issue.
  • Rave185Rave185 Posts: 28
    Lawnmower Man (or Woman)
    Hey Kevin! It took me a while, but I did finally get it to work so you can have an avatar walking around and grabbing things at the same time. I followed Ben Roberts youtube tutorial for the first bit, then kinda experimented with hierarchy settings after that. Here's the link to a copy of the project I was working on, if you wanna take a look at it yourself. Hope this helps! - http://bit.ly/2imJufh

    Still having trouble getting custom player avatars working though. The generic fallback avatar works, but no custom ones.

    (at the time of posting this the file is still uploading. Should be done in about an hour.)
  • KevinLongtimeKevinLongtime Posts: 282
    Nexus 6
    Thanks a ton, @Rave185 ! Once it's done uploading I'll give it a try when I'm home!
  • pedat2pedat2 Posts: 1
    Virtual Boy (or Girl)
    Does this code work with a Hinge Joint or a configurable joint?
  • AntonieBAntonieB Posts: 83
    Hiro Protagonist
    :) nice sample / learned some things from it. Thanks!!

    I know this is probably not a request thread but some simple sample with photon / pun / other networking layer would be awsome. In the mean time I'am trying myself to make this sample work with some simple PUN implementation.

    But again big thanks for this example.
  • CodexusCodexus Posts: 5
    Virtual Boy (or Girl)
    edited December 2016
    @Dorako, one solution is to put the physics time step at 1/90th of a second,  the drawbacks being that it could be cpu intensive for complex physics scenes and it might not be future proof as it couples the physics rate to the refresh rate of the HMD.

    As for the second point, I think it relates to a comment on the code where they say they have to update the hand position in OnFixedUpdate for physics to work. I think a possible solution would be to have the visible hand + object in hand drawn in OnUpdate() and not physics enabled while at the same time have an invisible version for physics in OnFixedUpdate(). (EDIT: At least as long as the object in hand is not touching anything, otherwise you need the physics interaction)

  • KevinLongtimeKevinLongtime Posts: 282
    Nexus 6
    edited December 2016
    Rave185 said:
    Hey Kevin! It took me a while, but I did finally get it to work so you can have an avatar walking around and grabbing things at the same time. I followed Ben Roberts youtube tutorial for the first bit, then kinda experimented with hierarchy settings after that. Here's the link to a copy of the project I was working on, if you wanna take a look at it yourself. Hope this helps! - http://bit.ly/2imJufh

    Still having trouble getting custom player avatars working though. The generic fallback avatar works, but no custom ones.

    (at the time of posting this the file is still uploading. Should be done in about an hour.)
    I tried it and it did indeed work! Thanks a ton man!

    Did you use your own grab code on this? @Rave185
  • ivialb21ivialb21 Posts: 4
    Virtual Boy (or Girl)
    Works perfectly except for the datarates not matching. Clicks don't synchronize with the grab location perceived.
  • clint205clint205 Posts: 2
    Virtual Boy (or Girl)
    Thanks so much for this!  What I am really also looking for is how to have the hand also grip the object correctly like in the Oculus First Contact tutorial.  I'm assuming a custom grip has to be made and I believe I have figured out how to make custom poses, I just need to know how to get it called when the gripping of an object occurs.
  • dvirdvir Posts: 3
    Virtual Boy (or Girl)
    @clint205: the best place for that would probably be in the Grabbable script, in GrabBegin and GrabEnd. You'll want to get a reference to the LocalAvatar's OvrAvatar component at that point, and then set avatar.RightHandCustomPose to the transform you made through this example: https://developer3.oculus.com/documentation/avatarsdk/latest/concepts/avatars-sdk-unity/#avatars-sdk-unity.xml-avatars-sdk-unity-custom-grip-poses

    For a really, really quick hack, drag the GameObject prefab you made from that tutorial to the Resources folder, call it for example "customHandPose", and then put this at the end of your Grabbable.GrabBegin call:

      GameObject.FindObjectOfType<OvrAvatar>().RightHandCustomPose = ((GameObject)Resources.Load("customHandPose")).transform;

    Let me know if that is still not clear :)
  • KevinLongtimeKevinLongtime Posts: 282
    Nexus 6
    Having a problem with grabbed objects losing parenting with artificial turning. Issue Occurs at 0:18





    Anything I'm overlooking? Tried parenting the avatar system to a bunch of different things and it doesn't solve this yet. Not only does it mess up an object you current have held but it messed up the detection for picking up other objects in the scene unless you resent orientation.
  • DorakoDorako Posts: 4
    Virtual Boy (or Girl)
    edited December 2016
    @Codexus Yeah I'm not sure upping the physics frequency that much and also tying it to HMD refresh rate is a good thing.
  • fancyfennecfancyfennec Posts: 11
    Virtual Boy (or Girl)
    Why is it so jittery when you hold an object? This should work even if Interpolate is set to none. I have a touch project with grab that I made from Ben Roberts' YouTube tutorial, and that works without the jitter. There has to be another solution than changing the Physics time step.
  • pjennesspjenness Posts: 652
    Neo
    Rave185 said:


    Still having trouble getting custom player avatars working though. The generic fallback avatar works, but no custom ones.






    Try starting with the OVR avatar disabled, use another class to poll the user ID of the player

    public string m_OVRappID = "";
    public User m_OVRuser = null;
    public ulong m_OVRuserID = 0;


    void Awake ()
    {
    Core.Initialize (m_OVRappID);
    doEntitlementCheck ();
    Users.GetLoggedInUser ().OnComplete (GetLoggedInUserCallback);
    }


    private void GetLoggedInUserCallback (Message msg)
    {
    if (!msg.IsError) {
    m_OVRuser = msg.GetUser ();
    m_OVRuserID = m_OVRuser.OculusID;
    }
    }





    Then on your ovrAvatar base character   (in my case I have a link to it m_OVRavatar)



    public OvrAvatar m_OVRavatar = null;


    void Awake ()
        {

            OvrAvatar ovrAvtr = this.GetComponent<OvrAvatar> ();
            if (ovrAvtr) {
               m_OVRavatar = ovrAvtr;
            }
        }



    once you have the UserID from earlier, pass it across and activate the Avatar. If I do it in this order it works,
    If I start already activated I get the dummy as the userID is 0. And I cant work out how to force a refresh if I change the userID



    m_OVRavatar.oculusUserID = m_myGameManager.m_myAppManager.m_OVRuserID;
    m_OVRavatar.enabled = true;



    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • pjennesspjenness Posts: 652
    Neo
    edited January 1

    Im having problems with the grabbers not sticking aligning to the touch hands.

    If I use the demo scene it seems to work fine. Ive extract the avatarWithGrab as a prefab and bring it into my game. Parented under a player controller heirarchy. Now the grabbers dont align.

    Im not sure if its heirarchy, or that Im useing "Eye Level" not :"Floor Level"   or the order I am activating things. But I cant get the grabbers to be in the correct hand positions.

    Update: Discovered that the rotation of a parent (ie the player controller) effects the "grabbers". At 0,0,0 they attach fine, once start rotating (say round Y) they mis-align.
    Not sure how to counteract that.

    OVRS Devs:   If you open the sample grab scene and play it works. Then if you grab the top of the ovrAvatar heirarch and rotate 90o you can see the grabbers do not rotate accordingly. They are are only usingthe local tracking info of the TrackingSpace , we need to add any additional heirarchy rotations on top. I think this should be a default check box, unless ther is already a way to do it?

    EDIT:  See next post for the line change


    -P




    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • pjennesspjenness Posts: 652
    Neo
    edited January 1
    I got it.

    Change the line in grabber.cs FixedUpdate to

    GetComponent<Rigidbody> ().MovePosition (m_parentTransform.rotation * ((handPos + m_anchorOffsetPosition)) + m_parentTransform.position);

    GetComponent<Rigidbody> ().MoveRotation (m_parentTransform.rotation * (handRot * m_anchorOffsetRotation));


    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • pjennesspjenness Posts: 652
    Neo
    edited January 2
    Is it just me, or has the original functionality of OVRTouch gone where a fist or pointed finger generated collision detection?
    I used to be able to punch rigid bodies, but not with the new Avatar based hands.


    Also something has changed tht breaks the mapping from mechanim IK hands to these new hands. THey previously worked well that you map the ik target of mechanim hands to the "Hand" from OVRtouch, but this doesnt work so well anymore. I have to dig 3 layers deep to find the hands:b_l_hand joint, to get pos. Rotation is next. Prob similar issue to above. (Vector math  urgh    :s )


    -P


    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • ColdSpikeColdSpike Posts: 6
    Virtual Boy (or Girl)
    dvir said:
    @clint205: the best place for that would probably be in the Grabbable script, in GrabBegin and GrabEnd. You'll want to get a reference to the LocalAvatar's OvrAvatar component at that point, and then set avatar.RightHandCustomPose to the transform you made through this example: https://developer3.oculus.com/documentation/avatarsdk/latest/concepts/avatars-sdk-unity/#avatars-sdk-unity.xml-avatars-sdk-unity-custom-grip-poses

    For a really, really quick hack, drag the GameObject prefab you made from that tutorial to the Resources folder, call it for example "customHandPose", and then put this at the end of your Grabbable.GrabBegin call:

      GameObject.FindObjectOfType<OvrAvatar>().RightHandCustomPose = ((GameObject)Resources.Load("customHandPose")).transform;

    Let me know if that is still not clear :)
    So is there an easier way to make custom poses? First off you obviously can't do that unless the scene is running, and I couldn't get it to work right because VR is enabled so I could barely see what I was rotating and not rotating + the shader  for the hands wouldn't render unless I move them about, which again makes it harder to tell what I'm animating.
  • ColdSpikeColdSpike Posts: 6
    Virtual Boy (or Girl)
    To anyone who approves comments, I didn't know they needed to be approved and the message which tells you they do only flashes for a split second so... sorry for posting so many times XD
  • mfmfmfmf Posts: 47 Oculus Staff
    NOTE: updated attached package to work well with OVRPlayerController and other things that move the avatar around.

  • mfmfmfmf Posts: 47 Oculus Staff
    pjenness said:
    Is it just me, or has the original functionality of OVRTouch gone where a fist or pointed finger generated collision detection?
    I used to be able to punch rigid bodies, but not with the new Avatar based hands.

    The custom hand sample will have that functionality when we release it later.
  • mfmfmfmf Posts: 47 Oculus Staff
    Why is it so jittery when you hold an object? This should work even if Interpolate is set to none. I have a touch project with grab that I made from Ben Roberts' YouTube tutorial, and that works without the jitter. There has to be another solution than changing the Physics time step.
    It's jittery because you're updating the object's position at a different frame rate than that of the HMD or of the hand. With that exact approach, it's going to be jittery.

    There are many other approaches. Parenting is a simple one. If you need solid physics, you can separate the visual mesh and rigid body, and parent the visual mesh while updating the rigid body at a lower frame rate.
  • Rave185Rave185 Posts: 28
    Lawnmower Man (or Woman)
    @pjenness

    Thanks for the code sample! Just having a bit of trouble, it doesn't recognize the word User and continues to error out. Any idea why that could be happening?
  • pjennesspjenness Posts: 652
    Neo
    edited January 5

    mfmf said:
    Why is it so jittery when you hold an object? This should work even if Interpolate is set to none. I have a touch project with grab that I made from Ben Roberts' YouTube tutorial, and that works without the jitter. There has to be another solution than changing the Physics time step.
    It's jittery because you're updating the object's position at a different frame rate than that of the HMD or of the hand. With that exact approach, it's going to be jittery.

    There are many other approaches. Parenting is a simple one. If you need solid physics, you can separate the visual mesh and rigid body, and parent the visual mesh while updating the rigid body at a lower frame rate.

    I managed to have the best of both worlds with a bit of a hack:
    https://forums.oculus.com/community/discussion/comment/478680/#Comment_478680

    The visual renderers become parented so looks smooth, and the collision objects remain outside, and use rigidbody movement to do correct collision detection.
    So far has been working well.

    -P

    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • pjennesspjenness Posts: 652
    Neo
    Rave185 said:
    @pjenness

    Thanks for the code sample! Just having a bit of trouble, it doesn't recognize the word User and continues to error out. Any idea why that could be happening?
    HIya

    Do you have this at the start of your script file?

    using Oculus.Platform.Models;


    -P
    Drift VFX Visual, Virtual , Vertical

    Want 970GTX on Macbook for good FPS?
    https://forums.oculus.com/viewtopic.php?f=26&t=17349
  • Rave185Rave185 Posts: 28
    Lawnmower Man (or Woman)
    you're very welcome @KevinLongtime ! Glad I could help! It's been about a week since I looked at it, so let me think about your second question for a sec... I believe most of it is offshoots from Ben Robert's tutorials on youtube, but yeah, I did change a couple things.
  • metrocubemetrocube Posts: 55 Oculus Staff
    edited January 12
    Rave185 said:
    Still having trouble getting custom player avatars working though. The generic fallback avatar works, but no custom ones.
    The Unity OvrAvatar is pre-wired to personalize avatars if you set the avatar's oculusUserID to a valid user ID. There's a trick though. Because the avatar gets created in the Start() function, you have to request and receive the user ID in an Awake() function. If you try to do it in Start(), it's too late.

    Note: Even with Awake(), you can still get into a race condition because the call to get the user ID is asynchronous. You don't know how long it's gonna take.

    The new docs will be live soon with some sample code that addresses all these gotchas in a straight-forward way.

    The sample adds a new class that initializes Oculus Platform in an Awake() function, requests the ID of the logged in user, and then waits until the callback function updates the oculusUserID value of your avatar.

    1. Import the Oculus Platform SDK's unity package into your avatar project.
    2. Get a real App ID from API tab of your app in the Developer Dashboard. If you haven't made an app there, go to https://dashboard.oculus.com to set yourself up as a game dev and create your first app.
    3. Copy your App ID string into the "Rift app ID" setting in the Oculus Avatars and Oculus Platform plugins.
    4. Add this script to an empty game object:
    PlatformManager.cs 

    using UnityEngine;
    using Oculus.Avatar;
    using Oculus.Platform;
    using Oculus.Platform.Models;
    using System.Collections;

    public class PlatformManager : MonoBehaviour {
        void Awake () {
            Oculus.Platform.Core.Initialize();
            Oculus.Platform.Users.GetLoggedInUser().OnComplete(GetLoggedInUserCallback);
            Oculus.Platform.Request.RunCallbacks(); //avoids race condition with OvrAvatar.cs Start().
        }

        private void GetLoggedInUserCallback(Message<User> message) {
            if (!message.IsError) {
                OvrAvatar[] avatars = FindObjectsOfType(typeof(OvrAvatar)) as OvrAvatar[];
                foreach (OvrAvatar avatar in avatars) {
                    avatar.oculusUserID = message.Data.ID;
                }
            }
          }
      }
  • Rave185Rave185 Posts: 28
    Lawnmower Man (or Woman)
    pjenness said:
    Rave185 said:
    @pjenness

    Thanks for the code sample! Just having a bit of trouble, it doesn't recognize the word User and continues to error out. Any idea why that could be happening?
    HIya

    Do you have this at the start of your script file?

    using Oculus.Platform.Models;


    -P

    Hmm... it says the namespace platform does not exist. Am I missing a package or something? Thx!
«1
Sign In or Register to comment.