diff --git a/Assets/Controller Right.prefab b/Assets/Controller Right.prefab index 58c2692..4985bea 100755 Binary files a/Assets/Controller Right.prefab and b/Assets/Controller Right.prefab differ diff --git a/Assets/FurnitureMiniature.cs b/Assets/FurnitureMiniature.cs index be4c75e..7cbfc0b 100755 --- a/Assets/FurnitureMiniature.cs +++ b/Assets/FurnitureMiniature.cs @@ -2,16 +2,12 @@ using System.Collections; public class FurnitureMiniature : MonoBehaviour { - public void Grab(Transform attachment) { + public void Grab() { transform.parent = null; foreach (var collider in GetComponentsInChildren()) { collider.enabled = false; } GetComponent().isKinematic = true; - - var mini = gameObject.AddComponent(); - mini.DestinationAttachment = attachment; - mini.Duration = 0.33f; } public void Release() { diff --git a/Assets/RightController.cs b/Assets/RightController.cs index 44bf79d..44ca02e 100755 --- a/Assets/RightController.cs +++ b/Assets/RightController.cs @@ -1,14 +1,17 @@ using UnityEngine; using System.Collections; using System; +using System.Collections.Generic; public class RightController : MonoBehaviour { public Transform MiniatureAttachmentPoint; SteamVR_LaserPointer laserPointer; SteamVR_ControllerEvents controllerEvents; + BoxCollider boxCollider; WarehouseColumn warehouse; + HashSet touchingItems = new HashSet(); GameObject heldItem; WarehouseRow whRow = null; @@ -48,11 +51,17 @@ public class RightController : MonoBehaviour { } } - RaycastHit raycast() { - var ray = new Ray(transform.position, transform.forward); - RaycastHit hit; - bool bHit = Physics.Raycast(ray, out hit); - return hit; + void OnTriggerEnter(Collider other) { + var mini = other.GetComponentInParent(); + if (mini == null) { + return; + } + + touchingItems.Add(other.gameObject); + } + + void OnTriggerExit(Collider other) { + touchingItems.Remove(other.gameObject); } // Furniture picking @@ -82,7 +91,28 @@ public class RightController : MonoBehaviour { } bool grabByProximity() { - return false; + GameObject closest = null; + foreach (var obj in touchingItems) { + if (closest == null) { + closest = obj; + continue; + } + + if ((obj.transform.position - boxCollider.bounds.center).magnitude < (closest.transform.position - boxCollider.bounds.center).magnitude) { + closest = obj; + } + } + + if (closest == null) { + return false; + } + + var mini = closest.GetComponentInParent(); + mini.Grab(); + mini.transform.parent = transform; + heldItem = mini.gameObject; + + return true; } bool grabByLaser() { @@ -98,18 +128,29 @@ public class RightController : MonoBehaviour { var warehouseItem = result.rigidbody.GetComponent(); if (warehouseItem) { var mini = warehouseItem.Pick(); - mini.Grab(MiniatureAttachmentPoint); - var anim = mini.GetComponent(); + mini.Grab(); + + var anim = mini.gameObject.AddComponent(); anim.OriginItem = warehouseItem; + anim.DestinationAttachment = MiniatureAttachmentPoint; anim.GoalScale = new Vector3(0.05f, 0.05f, 0.05f); + anim.Duration = 0.33f; + heldItem = mini.gameObject; + return true; } // Warehouse miniature grabbing remotely var furnitureMini = result.rigidbody.GetComponent(); if (furnitureMini) { - furnitureMini.Grab(MiniatureAttachmentPoint); + furnitureMini.Grab(); + + var anim = furnitureMini.gameObject.AddComponent(); + //anim.OriginItem = furnitureMini; + anim.DestinationAttachment = MiniatureAttachmentPoint; + anim.Duration = 0.33f; + heldItem = furnitureMini.gameObject; return true; } diff --git a/Assets/Test.unity b/Assets/Test.unity index 90a44d2..b11d8b9 100755 Binary files a/Assets/Test.unity and b/Assets/Test.unity differ