- Home /
Add a joint at collision point between two objects. (SteamVR held object interacting with a hinged object)
How can one attach a held object to a movable object in Steam VR? For example Lets say I pick up an item with the Steam VR throawable script, This item has a way to grab another object such as a hook or pincer. Once I am holding this object I would like to be able to collide with a large overhead (Normally out of reach) lever (With a linear or circular driver or a hing joint). The lever would swing around on one side while rotating it with the held object on the other side.
Any ideas on how this might be done?
Update: I am trying ↓ this ↓ with no luck.
using UnityEngine;
public class TaggedCollisionJoint : MonoBehaviour
{
public string linkLayerTag;
public float forceToBreak = 50;
public float torqueToBreak = 50;
// Start is called before the first frame update and allows the script to be disabled in the inspector.
void Start()
{
}
void OnCollisionEnter(Collision col)
{
// Set a tag to make the conection selective.
if (col.gameObject.tag == linkLayerTag)
{
//This ↓ works to make a solid connection between the tool and lever but breaks the lever off its joint.
//col.transform.parent = transform;
//This is not making a joint at all?
// create a joint -Tested with character, configurable, hinge and spring joints, but no joint is made.
CharacterJoint joint = gameObject.AddComponent<CharacterJoint>();
// sets joint position to point of contact
joint.anchor = col.contacts[0].point;
// conects the joint to the other object
joint.connectedBody = col.contacts[0].otherCollider.transform.GetComponentInParent<Rigidbody>();
//Set the forces which will break the joint.
joint.breakForce = forceToBreak;
joint.breakTorque = torqueToBreak;
// Stops objects from continuing to collide and creating more joints
joint.enableCollision = false;
}
}
}
The S$$anonymous$$m VR documentation says:
Objects can be attached to and detached from the Hand. Only one
object can be the object in focus of
the hand but multiple objects can be
attached to the hand at the same
time.
So if only one object can have focus but multiple objects can be attached... How would I use a tool in the players hand to interact with an otherwise out of reach object? I have been looking over documentation and testing things for days but I'm stuck and need some assistance from the community please.
I try to search this and come up with things like https://www.amazon.com/Vive-Reacher-Grabber-$$anonymous$$obility-Rotating/dp/B00O47NWE6. lol. Sure that's the tool I want to recreate virtually but in a Vive H$$anonymous$$D not a Vive Brand reacher grabber.
Answer by Giantbean · Dec 17, 2019 at 09:00 PM
This is working for me.
using UnityEngine;
public class TaggedCollisionJoint : MonoBehaviour
{
public string linkLayerTag;
public float forceToBreak = 50;
public float torqueToBreak = 50;
// Start is called before the first frame update and allows the script to be disabled in the inspector.
void Start()
{
}
void OnCollisionEnter(Collision col)
{
// Set a tag to make the conection selective.
if (col.gameObject.GetComponent<Rigidbody>() != null && col.gameObject.tag == linkLayerTag)
{
// create a joint
HingeJoint joint = gameObject.AddComponent<HingeJoint>();
//Set the anchor point where the wand and blade collide
ContactPoint contact = col.contacts[0];
joint.anchor = transform.InverseTransformPoint(contact.point);
joint.connectedBody = col.contacts[0].otherCollider.transform.GetComponent<Rigidbody>();
//Set the forces which will break the joint.
joint.breakForce = forceToBreak;
joint.breakTorque = torqueToBreak;
// Stops objects from continuing to collide and creating more joints
joint.enableCollision = false;
// ↑ This ↑ Sometimes still makes multiple joints which breaks things?
}
}
}