SHARE
TWEET

My hand script

a guest Feb 27th, 2019 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR.InteractionSystem;
  5. using Valve.VR;
  6.  
  7. public class HandScript : MonoBehaviour {
  8.  
  9.     public Transform playArea;
  10.     public Transform teleportTarget;
  11.     public Transform head;
  12.     public LayerMask teleportMask;
  13.  
  14.     public HandScript other;
  15.     [HideInInspector] ConfigurableJoint cj;
  16.     public SteamVR_Action_Boolean gripAction;
  17.     public SteamVR_Action_Boolean triggerAction;
  18.     public SteamVR_Action_Boolean dPadAction;
  19.  
  20.  
  21.     private GameObject inHand;
  22.     private Hand hand;
  23.     private Transform currentAnchor;
  24.     private LineRenderer lRend;
  25.     private WeaponScript weap;
  26.  
  27.     private IEnumerator calcVel;
  28.     private Vector3     currVel;
  29.     void Start() {
  30.         hand = GetComponent<Hand> ();
  31.         lRend = GetComponent<LineRenderer> ();
  32.         calcVel = calculateVelocity ();
  33.         StartCoroutine (calcVel);
  34.     }
  35.  
  36.     IEnumerator calculateVelocity() {
  37.         while (true) {
  38.             Vector3 posA = transform.position;
  39.             yield return null;
  40.             currVel = (transform.position - posA) / Time.deltaTime;
  41.         }
  42.     }
  43.  
  44.     void Update() {
  45.         if (dPadAction.GetStateDown (hand.handType)) {
  46.             if (VRManager.vrm.mainHand == hand.handType.ToString ()) {
  47.                 VRManager.vrm.toggleMode ();
  48.             } else {
  49.                 VRManager.vrm.toggleHandSide ();
  50.             }
  51.         }
  52.  
  53.         if (VRManager.vrm.teleportMode) {
  54.             if (weap != null) {
  55.                 weap.hideWeapon();
  56.             }
  57.  
  58.             if (VRManager.vrm.mainHand == hand.handType.ToString ()) {
  59.                 RaycastHit hit;
  60.  
  61.                 Vector3 fwd = transform.TransformDirection (Vector3.forward) * 10f;
  62.                 //Check if layer is really equal to teleportMask
  63.                 if (Physics.Raycast (transform.position, transform.forward, out hit, 100f, teleportMask) && hit.collider.gameObject.layer == LayerMask.NameToLayer("TeleportZone")) {
  64.                     teleportTarget.gameObject.SetActive (true);
  65.                     teleportTarget.position = hit.point;
  66.  
  67.                     lRend.enabled = true;
  68.                     Vector3[] points = getCurve (hit.point);
  69.                     lRend.positionCount = points.Length;
  70.                     lRend.SetPositions (points);
  71.  
  72.                     if (triggerAction.GetStateDown (hand.handType)) {
  73.                         playArea.position = hit.point + teleportOffset ();
  74.                     }
  75.                 } else {
  76.                     lRend.enabled = false;
  77.                     teleportTarget.gameObject.SetActive (false);
  78.                 }
  79.             } else {
  80.                 lRend.enabled = false;
  81.             }
  82.         } else {
  83.             lRend.enabled = false;
  84.             teleportTarget.gameObject.SetActive (false);
  85.             if (weap != null) {
  86.                 weap.showWeapon();
  87.             }
  88.  
  89.             if (gripAction.GetStateDown (hand.handType)){
  90.                 if (cj == null && inHand != null) {
  91.                     cj = createJoint(inHand);
  92.                 } else if (cj != null && cj.connectedBody != null){
  93.                     dropItem ();
  94.                 }
  95.             }
  96.  
  97.             if (weap != null && triggerAction.GetStateDown(hand.handType)) {
  98.                 weap.shoot ();
  99.             }
  100.         }
  101.  
  102.         if (cj != null && cj.connectedBody != null) {
  103.             cj.angularYMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
  104.             cj.angularZMotion = (other.cj == null || (other.cj != null && other.cj.connectedBody != cj.connectedBody)) ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
  105.         }
  106.     }
  107.  
  108.     public void dropItem() {
  109.         Rigidbody objRb = cj.connectedBody;
  110.         Throwable t = objRb.GetComponent<Throwable> ();
  111.         AnchorSetter anchors = objRb.GetComponent<AnchorSetter> ();
  112.  
  113.         if (weap != null) {
  114.             weap = null;
  115.         }
  116.  
  117.         if (t != null) {
  118.             t.active = true;
  119.         }
  120.  
  121.         if (anchors != null && currentAnchor != null) {
  122.             anchors.freeAnchor (currentAnchor);
  123.             currentAnchor = null;
  124.         }
  125.         cj.connectedBody = null;
  126.         objRb.velocity = (currVel * 5f);
  127.         Destroy (cj);
  128.         cj = null;
  129.     }
  130.  
  131.     Vector3 teleportOffset() {
  132.         return new Vector3 (playArea.position.x - head.position.x, 0f, playArea.position.z - head.position.z);
  133.     }
  134.  
  135.     ConfigurableJoint createJoint(GameObject obj) {
  136.         ConfigurableJoint joint = gameObject.AddComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
  137.         Rigidbody objRb = obj.GetComponent<Rigidbody>();
  138.         AnchorSetter anchors = obj.GetComponent<AnchorSetter> ();
  139.  
  140.         if (anchors != null) {
  141.             Transform nextAnchor = anchors.getNextAnchor ();
  142.             if (nextAnchor != null) {
  143.                 joint.autoConfigureConnectedAnchor = false;
  144.                 currentAnchor = nextAnchor;
  145.                 joint.anchor = Vector3.zero;
  146.                 joint.connectedAnchor = nextAnchor.localPosition;
  147.             }
  148.         }
  149.  
  150.         if (other.cj == null || other.cj.connectedBody != objRb) {
  151.             WeaponScript weapon = obj.GetComponent<WeaponScript> ();
  152.             if (weapon != null)
  153.                 weap = weapon; 
  154.            
  155.             obj.transform.eulerAngles = transform.eulerAngles;
  156.             joint.angularXMotion = ConfigurableJointMotion.Locked;
  157.             joint.angularYMotion = ConfigurableJointMotion.Locked;
  158.             joint.angularZMotion = ConfigurableJointMotion.Locked;
  159.         }
  160.            
  161.         joint.xMotion = ConfigurableJointMotion.Locked;
  162.         joint.yMotion = ConfigurableJointMotion.Locked;
  163.         joint.zMotion = ConfigurableJointMotion.Locked;
  164.  
  165.         joint.connectedBody = objRb;
  166.         return joint;
  167.     }
  168.  
  169.     Vector3[] getCurve(Vector3 target) {
  170.         List<Vector3> points = new List<Vector3> ();
  171.         for (float i = 0.0f; i < 1.0f; i += 0.1f){
  172.             points.Add(getBezierPoint(i, transform.position, target));
  173.         }
  174.  
  175.         return points.ToArray();
  176.     }
  177.  
  178.     Vector3 getBezierPoint(float t, Vector3 posA, Vector3 posB) {
  179.         Vector3 midPoint = Vector3.Lerp (posA, posB, 0.5f);
  180.         midPoint.y = posA.y + (Mathf.Clamp((posB - posA).magnitude, 0f, 1f) / 2f);
  181.         return Vector3.Lerp (Vector3.Lerp(posA, midPoint, t), Vector3.Lerp(midPoint, posB, t), t);
  182.     }
  183.  
  184.     void OnTriggerEnter(Collider col) {
  185.         if (col.gameObject != inHand && col.tag == "Interactable") {
  186.             inHand = col.gameObject;
  187.         }
  188.     }
  189.  
  190.     void OnTriggerExit(Collider col) {
  191.         if (col.gameObject == inHand) {
  192.             inHand = null;
  193.         }  
  194.     }
  195. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top