Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Valve.VR.InteractionSystem;
- using Valve.VR;
- public class HandScript : MonoBehaviour {
- public Transform playArea;
- public Transform teleportTarget;
- public Transform head;
- public LayerMask teleportMask;
- public HandScript other;
- [HideInInspector] ConfigurableJoint cj;
- public SteamVR_Action_Boolean gripAction;
- public SteamVR_Action_Boolean triggerAction;
- public SteamVR_Action_Boolean dPadAction;
- private GameObject inHand;
- private Hand hand;
- private Transform currentAnchor;
- private LineRenderer lRend;
- private WeaponScript weap;
- private IEnumerator calcVel;
- private Vector3 currVel;
- void Start() {
- hand = GetComponent<Hand> ();
- lRend = GetComponent<LineRenderer> ();
- calcVel = calculateVelocity ();
- StartCoroutine (calcVel);
- }
- IEnumerator calculateVelocity() {
- while (true) {
- Vector3 posA = transform.position;
- yield return null;
- currVel = (transform.position - posA) / Time.deltaTime;
- }
- }
- void Update() {
- if (dPadAction.GetStateDown (hand.handType)) {
- if (VRManager.vrm.mainHand == hand.handType.ToString ()) {
- VRManager.vrm.toggleMode ();
- } else {
- VRManager.vrm.toggleHandSide ();
- }
- }
- if (VRManager.vrm.teleportMode) {
- if (weap != null) {
- weap.hideWeapon();
- }
- if (VRManager.vrm.mainHand == hand.handType.ToString ()) {
- RaycastHit hit;
- Vector3 fwd = transform.TransformDirection (Vector3.forward) * 10f;
- //Check if layer is really equal to teleportMask
- if (Physics.Raycast (transform.position, transform.forward, out hit, 100f, teleportMask) && hit.collider.gameObject.layer == LayerMask.NameToLayer("TeleportZone")) {
- teleportTarget.gameObject.SetActive (true);
- teleportTarget.position = hit.point;
- lRend.enabled = true;
- Vector3[] points = getCurve (hit.point);
- lRend.positionCount = points.Length;
- lRend.SetPositions (points);
- if (triggerAction.GetStateDown (hand.handType)) {
- playArea.position = hit.point + teleportOffset ();
- }
- } else {
- lRend.enabled = false;
- teleportTarget.gameObject.SetActive (false);
- }
- } else {
- lRend.enabled = false;
- }
- } else {
- lRend.enabled = false;
- teleportTarget.gameObject.SetActive (false);
- if (weap != null) {
- weap.showWeapon();
- }
- if (gripAction.GetStateDown (hand.handType)){
- if (cj == null && inHand != null) {
- cj = createJoint(inHand);
- } else if (cj != null && cj.connectedBody != null){
- dropItem ();
- }
- }
- if (weap != null && triggerAction.GetStateDown(hand.handType)) {
- weap.shoot ();
- }
- }
- 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;
- }
- }
- public void dropItem() {
- Rigidbody objRb = cj.connectedBody;
- Throwable t = objRb.GetComponent<Throwable> ();
- AnchorSetter anchors = objRb.GetComponent<AnchorSetter> ();
- if (weap != null) {
- weap = null;
- }
- if (t != null) {
- t.active = true;
- }
- if (anchors != null && currentAnchor != null) {
- anchors.freeAnchor (currentAnchor);
- currentAnchor = null;
- }
- cj.connectedBody = null;
- objRb.velocity = (currVel * 5f);
- Destroy (cj);
- cj = null;
- }
- Vector3 teleportOffset() {
- return new Vector3 (playArea.position.x - head.position.x, 0f, playArea.position.z - head.position.z);
- }
- 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) {
- WeaponScript weapon = obj.GetComponent<WeaponScript> ();
- if (weapon != null)
- weap = weapon;
- 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;
- }
- Vector3[] getCurve(Vector3 target) {
- List<Vector3> points = new List<Vector3> ();
- for (float i = 0.0f; i < 1.0f; i += 0.1f){
- points.Add(getBezierPoint(i, transform.position, target));
- }
- return points.ToArray();
- }
- Vector3 getBezierPoint(float t, Vector3 posA, Vector3 posB) {
- Vector3 midPoint = Vector3.Lerp (posA, posB, 0.5f);
- midPoint.y = posA.y + (Mathf.Clamp((posB - posA).magnitude, 0f, 1f) / 2f);
- return Vector3.Lerp (Vector3.Lerp(posA, midPoint, t), Vector3.Lerp(midPoint, posB, t), t);
- }
- 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