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. ![]()
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;
}
}
}