SHARE
TWEET

Untitled

a guest Feb 28th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public class HandScript : MonoBehaviour {
  2.    
  3.         public HandScript other;
  4.         [HideInInspector] ConfigurableJoint cj;
  5.         public SteamVR_Action_Boolean gripAction;
  6.    
  7.         private GameObject inHand;
  8.         private Hand hand;
  9.         private Transform currentAnchor;
  10.    
  11.         void Start() {
  12.             hand = GetComponent<Hand>();
  13.         }
  14.    
  15.         void Update() {
  16.             if (gripAction.GetStateDown (hand.handType)){
  17.                 if (cj == null && inHand != null) {
  18.                     cj = createJoint(inHand);
  19.                 }
  20.             }
  21.    
  22.             if (cj != null && cj.connectedBody != null) {
  23.                 cj.angularYMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
  24.                 cj.angularZMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
  25.             }
  26.         }
  27.    
  28.         ConfigurableJoint createJoint(GameObject obj) {
  29.             ConfigurableJoint joint = gameObject.AddComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
  30.             Rigidbody objRb = obj.GetComponent<Rigidbody>();
  31.             AnchorSetter anchors = obj.GetComponent<AnchorSetter> ();
  32.    
  33.             if (anchors != null) {
  34.                 Transform nextAnchor = anchors.getNextAnchor();
  35.    
  36.                 if (nextAnchor != null) {
  37.                     joint.autoConfigureConnectedAnchor = false;
  38.                     currentAnchor = nextAnchor;
  39.                     joint.anchor = Vector3.zero;
  40.                     joint.connectedAnchor = nextAnchor.localPosition;
  41.                 }
  42.             }
  43.    
  44.             if (other.cj == null || other.cj.connectedBody != objRb) {
  45.                 obj.transform.eulerAngles = transform.eulerAngles;
  46.                 joint.angularXMotion = ConfigurableJointMotion.Locked;
  47.                 joint.angularYMotion = ConfigurableJointMotion.Locked;
  48.                 joint.angularZMotion = ConfigurableJointMotion.Locked;
  49.             }
  50.                
  51.             joint.xMotion = ConfigurableJointMotion.Locked;
  52.             joint.yMotion = ConfigurableJointMotion.Locked;
  53.             joint.zMotion = ConfigurableJointMotion.Locked;
  54.    
  55.             joint.connectedBody = objRb;
  56.             return joint;
  57.         }
  58.    
  59.         void OnTriggerEnter(Collider col) {
  60.             if (col.gameObject != inHand && col.tag == "Interactable") {
  61.                 inHand = col.gameObject;
  62.             }
  63.         }
  64.    
  65.         void OnTriggerExit(Collider col) {
  66.             if (col.gameObject == inHand) {
  67.                 inHand = null;
  68.             }  
  69.         }
  70.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top