private Vector3 CalculateActivationPoint() { Bounds bounds = Utilities.getBounds(transform); float extents = 0; switch (finalDirection) { case Direction.x: case Direction.negX: extents = bounds.extents.x; break; case Direction.y: case Direction.negY: extents = bounds.extents.y; break; case Direction.z: case Direction.negZ: extents = bounds.extents.z; break; } // subtract width of button return(bounds.center + -GetForceVector().normalized *(extents + activationDistance)); }
private float calculateValue() { Vector3 center = (direction == Direction.autodetect) ? Utilities.getBounds(transform).center : transform.position; float dist1 = Vector3.Distance(finalMinPoint, center); float dist2 = Vector3.Distance(center, finalMaxPoint); return(Mathf.Round((min + Mathf.Clamp01(dist1 / (dist1 + dist2)) * (max - min)) / stepSize) * stepSize); }
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 bool doDetectMinMax(Direction direction) { Bounds bounds = Utilities.getBounds(transform); Vector3 v1 = Vector3.zero; Vector3 v2 = Vector3.zero; float extents = 0; switch (direction) { case Direction.x: v1 = Vector3.left; v2 = Vector3.right; extents = bounds.extents.x; break; case Direction.y: v1 = Vector3.down; v2 = Vector3.up; extents = bounds.extents.y; break; case Direction.z: v1 = Vector3.forward; v2 = Vector3.back; extents = bounds.extents.z; break; } RaycastHit hit1; RaycastHit hit2; Physics.Raycast(bounds.center, v1, out hit1, extents * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, v2, out hit2, extents * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); if (hit1.collider && hit2.collider) { finalMinPoint = hit1.point; finalMaxPoint = hit2.point; // subtract width of slider to reach all values finalMinPoint = finalMinPoint + (finalMaxPoint - finalMinPoint).normalized * extents; finalMaxPoint = finalMaxPoint - (finalMaxPoint - finalMinPoint).normalized * extents; return(true); } else { finalMinPoint = transform.position; finalMaxPoint = transform.position; } return(false); }
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); }
private Direction detectDirection() { Direction direction = Direction.autodetect; Bounds bounds = Utilities.getBounds(transform); // shoot rays from the center of the slider, this means the center should be inside the frame to work properly 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_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.back, out hitBack, bounds.extents.z * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.left, out hitLeft, bounds.extents.x * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.right, out hitRight, bounds.extents.x * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.up, out hitUp, bounds.extents.y * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); Physics.Raycast(bounds.center, Vector3.down, out hitDown, bounds.extents.y * MAX_AUTODETECT_SLIDER_LENGTH, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal); // shortest valid ray pair identifies first axis float lengthX = (hitLeft.collider != null && hitRight.collider != null) ? hitLeft.distance + hitRight.distance : float.MaxValue; float lengthY = (hitUp.collider != null && hitDown.collider != null) ? hitUp.distance + hitDown.distance : float.MaxValue; float lengthZ = (hitForward.collider != null && hitBack.collider != null) ? hitForward.distance + hitBack.distance : float.MaxValue; if (lengthX < lengthY & lengthX < lengthZ) { if (lengthY < lengthZ) { direction = Direction.y; } else if (lengthZ < lengthY) { direction = Direction.z; } else // onset { direction = Direction.x; } } if (lengthY < lengthX & lengthY < lengthZ) { if (lengthX < lengthZ) { direction = Direction.x; } else if (lengthZ < lengthX) { direction = Direction.z; } else // onset { direction = Direction.y; } } if (lengthZ < lengthX & lengthZ < lengthY) { if (lengthX < lengthY) { direction = Direction.x; } else if (lengthY < lengthX) { direction = Direction.y; } else // onset { direction = Direction.z; } } if (direction != Direction.autodetect) { if (!doDetectMinMax(direction)) { direction = Direction.autodetect; } } else { finalMinPoint = transform.position; finalMaxPoint = transform.position; } return(direction); }