cancel
Showing results for 
Search instead for 
Did you mean: 

Simple Grab Script

WickedManatee
Protege
Hey
So I've been trying to implement a simple spawn and/or grab script. The issue I'm having seems to be that the objects grabbed aren't moving with the avatar hands.
1, Is there a special way needed to used FixedJoints?  After I instantiate or move the object to my hand, it doesn't update with my hands..bceyyayi5p2o.gif.
I have a PickupThrow.cs on LocalAvatar gameobject
 void FixedUpdate()
    {
        if (joint == null) //joint is a private FixedJoint
        {
          GameObject go;
            if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger))
            { //throwableObject is a public gameobject to track if existing object is grabbed. if not, we create one at hand
                go = (throwableObject == null) ?
                    GameObject.Instantiate(Persistent.instance.gameManager.objPrefab) : throwableObject;
                go.transform.position = leftHand.transform.position; //leftHand is hand_left from LocalAvatar. used in script as a public GameObject
                go.GetComponent<ObjManager>().IsGrabbed(); //turns off gravity on obj, sounds, etc
                joint = go.AddComponent<FixedJoint>();
                joint.connectedBody = leftHand.GetComponent<Rigidbody>(); //added rigidbody without gravity and a boxcollider trigger to the left_hand from LocalAvatar
            }
            ...
I also tried adding the joint to the hand and making the grenade the connected body with no success.

2.What's the best practice for things that move the player? I added a Rigidbody with gravity and a box collider to my OVRCameraRig so I can have other things affect it. To keep it aligned with the avatar, I dragged the LocalAvatar to be a child of the rig. I'm not sure if this will be ideal when I get the grabbing and throwing worked out since they can get in the way. You can see the body being pushed or nades being pushed in the gif because of colliders

Thanks for any input
1 ACCEPTED SOLUTION

Accepted Solutions

delphinius81
Rising Star
1. If you don't child the instantiated object to the hands, the child object's position won't update automatically. So you need to add go.transform.parent = leftHand.transform. Then for throwing, you can determine when enough force has been applied to break your joint, at which point you can remove the leftHand as the parent of the game object.

2. Using collision detection on the player can lead to sickness (due to the player moving physically, but the visual system not moving - so you get a vestibular mismatch), so it's better to just let the player clip through walls.

View solution in original post

6 REPLIES 6

delphinius81
Rising Star
1. If you don't child the instantiated object to the hands, the child object's position won't update automatically. So you need to add go.transform.parent = leftHand.transform. Then for throwing, you can determine when enough force has been applied to break your joint, at which point you can remove the leftHand as the parent of the game object.

2. Using collision detection on the player can lead to sickness (due to the player moving physically, but the visual system not moving - so you get a vestibular mismatch), so it's better to just let the player clip through walls.

WickedManatee
Protege
1. Ah yeah, parenting it worked. I guess fixed joints don't really do anything in this case.
I'm determining the throw based on TouchUp. Allows for small or large throws

2. I'm not really worried about the wall glitching. I'm more interested in when other objects can affect the player. Darth Vader force pushing you, skating on a hoverboard, etc. Would the strategy be to Instantiate an invisible object and child the player to it so Vader pushes the object instead? Apply motion to the hoverboard after childing the player to it? I understand the motion sickness aspect, but I don't think it's good to just remove the possibility altogether. I'd lock the rotation from being affected, but I don't see an issue with moving the position. Or should I keep the collider on Player and dynamically check if the other object is something I want to ignore OnCollisionEnter and use Physics.IgnoreCollision?

WickedManatee
Protege
@delphinius81 not sure if you get notifications on threads marked as answered so I'm going to tag you. Do you have any other comments on having outside locomotion on the player object?

delphinius81
Rising Star
Even moving the player's position can lead to sickness - not everyone will be affected by it, but it's one of the main causes. If you do move the player, make sure that you use a constant velocity (no acceleration). How you move the player will depend on what makes sense. For vehicle locomotion, you might child the player's camera to the moving object. For knockback effects, assuming you are using an OVRCameraRig prefab, you could apply the knockback on the top level game object of the prefab - that would also move the cameras attached to the eye tracker child nodes.

The way you implement these kinds of things in Unity isn't any different in VR than it would be in a non-VR game - the only thing that's different here is whether you should.

Gravedrinker
Honored Guest
Could you post the entire script? I'm comparing the SteamVR integration and this and I must have forgotten something when attempting to do it with this.

WickedManatee
Protege
Yeah, I understand that. I feel like they are different though with the implementation of the hands. Granted, this issue doesn't come into play if you don't allow moving the player via outside means. Maybe I just need to tighten up the player's body collider to avoid issues with other colliders being moved by hands. Thanks for the input.

Grave, if you have a specific question, I can help you out. The only thing really different from SteamVR is the button casting. The rest is just making sure the right colliders/rigidbodies are on the objects in Unity