private Direction detectDirection() { Direction direction = Direction.x; Bounds bounds = Utilities.getBounds(transform); // shoot rays in all directions to learn about surroundings RaycastHit hitForward; RaycastHit hitBack; RaycastHit hitLeft; RaycastHit hitRight; RaycastHit hitUp; RaycastHit hitDown; Physics.Raycast(bounds.center, Vector3.forward, out hitForward, bounds.extents.z * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.back, out hitBack, bounds.extents.z * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.left, out hitLeft, bounds.extents.x * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.right, out hitRight, bounds.extents.x * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.up, out hitUp, bounds.extents.y * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.down, out hitDown, bounds.extents.y * MAX_AUTODETECT_KNOB_WIDTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); // shortest valid ray wins float lengthX = (hitRight.collider != null) ? hitRight.distance : float.MaxValue; float lengthY = (hitDown.collider != null) ? hitDown.distance : float.MaxValue; float lengthZ = (hitBack.collider != null) ? hitBack.distance : float.MaxValue; float lengthNegX = (hitLeft.collider != null) ? hitLeft.distance : float.MaxValue; float lengthNegY = (hitUp.collider != null) ? hitUp.distance : float.MaxValue; float lengthNegZ = (hitForward.collider != null) ? hitForward.distance : float.MaxValue; // TODO: not yet the right decision strategy, works only partially Vector3 hitPoint = Vector3.zero; if (Utilities.isLowest(lengthX, new float[] { lengthY, lengthZ, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.z; } else if (Utilities.isLowest(lengthY, new float[] { lengthX, lengthZ, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.y; } else if (Utilities.isLowest(lengthZ, new float[] { lengthX, lengthY, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.x; } else if (Utilities.isLowest(lengthNegX, new float[] { lengthX, lengthY, lengthZ, lengthNegY, lengthNegZ })) { direction = Direction.z; } else if (Utilities.isLowest(lengthNegY, new float[] { lengthX, lengthY, lengthZ, lengthNegX, lengthNegZ })) { direction = Direction.y; } else if (Utilities.isLowest(lengthNegZ, new float[] { lengthX, lengthY, lengthZ, lengthNegX, lengthNegY })) { direction = Direction.x; } return(direction); }
private Direction detectDirection() { Direction direction = Direction.autodetect; Bounds bounds = Utilities.getBounds(transform); // shoot rays from the center of the button to learn about surroundings RaycastHit hitForward; RaycastHit hitBack; RaycastHit hitLeft; RaycastHit hitRight; RaycastHit hitUp; RaycastHit hitDown; Physics.Raycast(bounds.center, Vector3.forward, out hitForward, bounds.extents.z * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.back, out hitBack, bounds.extents.z * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.left, out hitLeft, bounds.extents.x * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.right, out hitRight, bounds.extents.x * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.up, out hitUp, bounds.extents.y * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.down, out hitDown, bounds.extents.y * MAX_AUTODETECT_ACTIVATION_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); // shortest valid ray wins float lengthX = (hitRight.collider != null) ? hitRight.distance : float.MaxValue; float lengthY = (hitDown.collider != null) ? hitDown.distance : float.MaxValue; float lengthZ = (hitBack.collider != null) ? hitBack.distance : float.MaxValue; float lengthNegX = (hitLeft.collider != null) ? hitLeft.distance : float.MaxValue; float lengthNegY = (hitUp.collider != null) ? hitUp.distance : float.MaxValue; float lengthNegZ = (hitForward.collider != null) ? hitForward.distance : float.MaxValue; float extents = 0; Vector3 hitPoint = Vector3.zero; if (Utilities.isLowest(lengthX, new float[] { lengthY, lengthZ, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.negX; hitPoint = hitRight.point; extents = bounds.extents.x; } else if (Utilities.isLowest(lengthY, new float[] { lengthX, lengthZ, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.y; hitPoint = hitDown.point; extents = bounds.extents.y; } else if (Utilities.isLowest(lengthZ, new float[] { lengthX, lengthY, lengthNegX, lengthNegY, lengthNegZ })) { direction = Direction.z; hitPoint = hitBack.point; extents = bounds.extents.z; } else if (Utilities.isLowest(lengthNegX, new float[] { lengthX, lengthY, lengthZ, lengthNegY, lengthNegZ })) { direction = Direction.x; hitPoint = hitLeft.point; extents = bounds.extents.x; } else if (Utilities.isLowest(lengthNegY, new float[] { lengthX, lengthY, lengthZ, lengthNegX, lengthNegZ })) { direction = Direction.negY; hitPoint = hitUp.point; extents = bounds.extents.y; } else if (Utilities.isLowest(lengthNegZ, new float[] { lengthX, lengthY, lengthZ, lengthNegX, lengthNegY })) { direction = Direction.negZ; hitPoint = hitForward.point; extents = bounds.extents.z; } // determin activation distance activationDistance = (Vector3.Distance(hitPoint, bounds.center) - extents) * 0.95f; if (direction == Direction.autodetect || activationDistance < 0) { // auto-detection was not possible or colliding with object already direction = Direction.autodetect; activationDistance = 0; } return(direction); }