| 1 | using System.Collections; |
| 2 | using System.Collections.Generic; |
| 3 | using UnityEngine; |
| 4 | |
| 5 | public class ObjectGrabber : MonoBehaviour |
| 6 | { |
| 7 | [SerializeField] private Controllers type; |
| 8 | private Grabbable grabbed; |
| 9 | private Transform oldParent; |
| 10 | |
| 11 | private float grabDistance = 0.2f; |
| 12 | |
| 13 | private Vector3 oldPosition = Vector3.zero; |
| 14 | private Vector3 oldRotation; |
| 15 | |
| 16 | private float activateTimer = 0; |
| 17 | |
| 18 | public enum Controllers |
| 19 | { |
| 20 | Primary_Left, Secondary_Right |
| 21 | } |
| 22 | |
| 23 | private bool grab = false; |
| 24 | |
| 25 | // Update is called once per frame |
| 26 | void Update() |
| 27 | { |
| 28 | activateTimer -= Time.deltaTime; |
| 29 | bool flip = false; |
| 30 | |
| 31 | if (activateTimer <= 0) |
| 32 | { |
| 33 | if (type == Controllers.Primary_Left) |
| 34 | { |
| 35 | flip = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger) > 0.5f; |
| 36 | } |
| 37 | |
| 38 | if (type == Controllers.Secondary_Right) |
| 39 | { |
| 40 | flip = OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) > 0.5f; |
| 41 | } |
| 42 | |
| 43 | if (flip && grabbed == null) |
| 44 | { |
| 45 | Connectable[] bodies = FindObjectsOfType<Connectable>(); |
| 46 | |
| 47 | Connectable body = null; |
| 48 | float d = grabDistance * 1.6f; |
| 49 | |
| 50 | for (int i = 0; i < bodies.Length; i++) |
| 51 | { |
| 52 | float dist = Vector3.Distance(transform.position, bodies[i].transform.position); |
| 53 | |
| 54 | if (dist < d && !bodies[i].Locked) |
| 55 | { |
| 56 | body = bodies[i]; |
| 57 | d = dist; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | body.Activate(); |
| 62 | activateTimer = 0.6f; |
| 63 | } |
| 64 | } |
| 65 | // ^ACTIVATE vGRAB |
| 66 | if (grab == false) |
| 67 | { |
| 68 | if(grabbed != null) |
| 69 | { |
| 70 | grabbed.Release((transform.position - oldPosition) * 100, (transform.rotation.eulerAngles - oldRotation) * 2); |
| 71 | grabbed = null; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (type == Controllers.Primary_Left) |
| 76 | { |
| 77 | grab = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) > 0.5f; |
| 78 | } |
| 79 | |
| 80 | if (type == Controllers.Secondary_Right) |
| 81 | { |
| 82 | grab = OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger) > 0.5f; |
| 83 | } |
| 84 | |
| 85 | if (grab && grabbed == null) |
| 86 | { |
| 87 | Grabbable[] bodies = FindObjectsOfType<Grabbable>(); |
| 88 | |
| 89 | Grabbable body = null; |
| 90 | float d = grabDistance; |
| 91 | |
| 92 | for(int i = 0; i < bodies.Length; i++) |
| 93 | { |
| 94 | float dist = Vector3.Distance(transform.position, bodies[i].transform.position); |
| 95 | |
| 96 | if(dist < d && !bodies[i].IsGrabbed && !bodies[i].Locked) |
| 97 | { |
| 98 | body = bodies[i]; |
| 99 | d = dist; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | grabbed = body; |
| 104 | |
| 105 | if(grabbed != null) |
| 106 | { |
| 107 | grabbed.Grab(this); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | oldPosition = transform.position; |
| 112 | oldRotation = transform.rotation.eulerAngles; |
| 113 | } |
| 114 | } |