Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HandScript : MonoBehaviour {
- public HandScript other;
- [HideInInspector] ConfigurableJoint cj;
- public SteamVR_Action_Boolean gripAction;
- private GameObject inHand;
- private Hand hand;
- private Transform currentAnchor;
- void Start() {
- hand = GetComponent<Hand>();
- }
- void Update() {
- if (gripAction.GetStateDown (hand.handType)){
- if (cj == null && inHand != null) {
- cj = createJoint(inHand);
- }
- }
- if (cj != null && cj.connectedBody != null) {
- cj.angularYMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
- cj.angularZMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
- }
- }
- ConfigurableJoint createJoint(GameObject obj) {
- ConfigurableJoint joint = gameObject.AddComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
- Rigidbody objRb = obj.GetComponent<Rigidbody>();
- AnchorSetter anchors = obj.GetComponent<AnchorSetter> ();
- if (anchors != null) {
- Transform nextAnchor = anchors.getNextAnchor();
- if (nextAnchor != null) {
- joint.autoConfigureConnectedAnchor = false;
- currentAnchor = nextAnchor;
- joint.anchor = Vector3.zero;
- joint.connectedAnchor = nextAnchor.localPosition;
- }
- }
- if (other.cj == null || other.cj.connectedBody != objRb) {
- obj.transform.eulerAngles = transform.eulerAngles;
- joint.angularXMotion = ConfigurableJointMotion.Locked;
- joint.angularYMotion = ConfigurableJointMotion.Locked;
- joint.angularZMotion = ConfigurableJointMotion.Locked;
- }
- joint.xMotion = ConfigurableJointMotion.Locked;
- joint.yMotion = ConfigurableJointMotion.Locked;
- joint.zMotion = ConfigurableJointMotion.Locked;
- joint.connectedBody = objRb;
- return joint;
- }
- void OnTriggerEnter(Collider col) {
- if (col.gameObject != inHand && col.tag == "Interactable") {
- inHand = col.gameObject;
- }
- }
- void OnTriggerExit(Collider col) {
- if (col.gameObject == inHand) {
- inHand = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement