picking up object in VR at a point like the grip of a gun.

I have a simple grabbing system that I’m using to manipulate objects in VR. I really like the way it works but all objects snap to the hands at their center point. I’m really new to unity, is there a simple way to change this bit of code to make objects snap to my hands at a different point? I’d like to be able to hold guns by the grip or swords by the hilt.

Thanks for any help you can provide. If this is common knowledge I should have and you can recommend a resource to learn from that would be awesome too. :slight_smile:

void GrabObject()
    {
        grabbing = true;
        setHandsClosed();

        RaycastHit[] hits;

        hits = Physics.SphereCastAll(transform.position, grabRadius, transform.forward, 0f, grabMask);

        if (hits.Length > 0)
        {
            int closestHit = 0;
            for (int i = 0; i < hits.Length; i++)
            {

                if (hits[i].distance < hits[closestHit].distance)
                {                   
                    closestHit = i;                   
                }
            }

            //test if youre already holding the object in another hand
            if (hits[closestHit].transform.gameObject.GetComponent<grabbableObject>().held == false)
            {
                //move object to your hand
                grabbedObject = hits[closestHit].transform.gameObject;
                grabbedObject.GetComponent<Rigidbody>().isKinematic = true;
                grabbedObject.transform.position = transform.position;
                grabbedObject.transform.parent = transform;
                grabbedObject.GetComponent<grabbableObject>().held = true;
            }

        }
    }

Put the grabbableObject script on a parent of the main object (you can use GetComponentInParent), and then you can reposition its child object as you see fit.

If you want to do it the easy way, you could use VRTK

How do you use snap grabbing with that

1 Like

for all you people just now seeing this thread, there are some gun grip pickups in example scene 5 on a table to the right. There’s also a foregrip example at the very far end of the tables.

I might have missed something here, which asset has these pickups please?