using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectGrabber : MonoBehaviour { [SerializeField] private Controllers type; private Grabbable grabbed; private Transform oldParent; private float grabDistance = 0.2f; private Vector3 oldPosition = Vector3.zero; private Vector3 oldRotation; public enum Controllers { Primary_Left, Secondary_Right } private bool grab = false; // Update is called once per frame void Update() { if (grab == false) { if(grabbed != null) { grabbed.Release((transform.position - oldPosition) * 100, (transform.rotation.eulerAngles - oldRotation) * 2); grabbed = null; } } if (type == Controllers.Primary_Left) { grab = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger) > 0.5f; } if (type == Controllers.Secondary_Right) { grab = OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) > 0.5f; } if (grab && grabbed == null) { Grabbable[] bodies = FindObjectsOfType(); Grabbable body = null; float d = grabDistance; for(int i = 0; i < bodies.Length; i++) { float dist = Vector3.Distance(transform.position, bodies[i].transform.position); if(dist < d) { body = bodies[i]; d = dist; } } grabbed = body; if(grabbed != null) { grabbed.Grab(this); } } oldPosition = transform.position; oldRotation = transform.rotation.eulerAngles; } }