using System.Collections; using UnityEngine; using Valve.VR.InteractionSystem; public class CatalogElement : Throwable { public GameObject largeFurnitureModel; public int index; private GameObject smallFurnitureModel; private Vector3 returnPosition; // Start is called before the first frame update void Start() { returnPosition = transform.localPosition; } // Update is called once per frame void Update() { } protected override void OnAttachedToHand(Hand hand) { //Debug.Log("[SteamVR Interaction] Pickup: " + hand.GetGrabStarting().ToString()); hadInterpolation = this.rigidbody.interpolation; attached = true; onPickUp.Invoke(); hand.HoverLock(null); rigidbody.interpolation = RigidbodyInterpolation.None; attachTime = Time.time; attachPosition = transform.position; attachRotation = transform.rotation; Catalog.Instance.OnGrabSphere(this, hand); //hand.GetComponent().Grabbing = largeFurnitureModel.GetComponent(); } //------------------------------------------------- protected override void OnDetachedFromHand(Hand hand) { attached = false; onDetachFromHand.Invoke(); hand.HoverUnlock(null); rigidbody.interpolation = hadInterpolation; Catalog.Instance.OnReleaseSphere(this, hand); Destroy(gameObject); } public void SetFurniture(GameObject furniture, float objectScale) { largeFurnitureModel = furniture; smallFurnitureModel = Instantiate(furniture); var throwables = smallFurnitureModel.GetComponentsInChildren(); foreach (Throwable throwable in throwables) { Destroy(throwable); }; var rigidbodies = smallFurnitureModel.GetComponentsInChildren(); foreach (Rigidbody rigidbody in rigidbodies) { Destroy(rigidbody); }; smallFurnitureModel.transform.parent = transform; Destroy(smallFurnitureModel.GetComponent()); Furniture objectBounds = smallFurnitureModel.AddComponent(); objectBounds.CalculateBounds(); var colldiers = smallFurnitureModel.GetComponentsInChildren(); foreach (Collider collider in colldiers) { Destroy(collider); }; float rescale = (objectScale / 1.4f) / objectBounds.combinedBounds.size.magnitude; smallFurnitureModel.transform.localScale *= rescale; smallFurnitureModel.transform.localPosition = Vector3.down * 0.2f; StartCoroutine(KeepFurnitureAligned()); } public void ClearFurniture() { Destroy(smallFurnitureModel); } private IEnumerator ReturnSphere() { float time = 0; Vector3 startPosition = transform.localPosition; while (time <= 1) { transform.localPosition = Vector3.Lerp(startPosition, returnPosition, time); time += Time.deltaTime * 4; yield return null; } yield break; } private IEnumerator KeepFurnitureAligned() { while (true) { smallFurnitureModel.transform.eulerAngles = new Vector3(0, smallFurnitureModel.transform.eulerAngles.y - (20 * Time.deltaTime), 0); yield return null; } } private void OnPointerEnter() { Debug.Log("enter"); } }