如何获取ARKit 1.5(Unity)中墙面生成的HitCube尺寸?
Fix: Getting Accurate HitCube Dimensions with ARKit 1.5 in Unity
Hey there! I’ve run into this exact problem when building AR apps with ARKit 1.5 and Unity, so let me break down the reliable ways to grab those HitCube dimensions correctly.
First, Clarify Your HitCube Source
The approach depends on whether your HitCube is generated directly from ARKit’s plane data, or if it’s a custom GameObject you spawn at the hit location. Let’s cover both cases:
Case 1: HitCube Tied to ARKit’s ARPlaneAnchor
ARKit 1.5 introduced better vertical plane (wall) detection, and the ARPlaneAnchor holds all the critical size data for detected planes. Here’s how to pull it:
- Make sure your hit test targets the right plane type. Use
ARHitTestResultType.ExistingPlaneUsingExtentfor consistent, bounded plane data (this is better thanExistingPlaneUsingGeometryif you need a defined cube size). - Extract the anchor from your hit result and access its
sizeproperty:
// Assume you have a valid ARHitTestResult from your raycast ARHitTestResult hitResult = YourHitTestLogic(); if (hitResult.type == ARHitTestResultType.ExistingPlaneUsingExtent) { ARPlaneAnchor planeAnchor = hitResult.anchor as ARPlaneAnchor; if (planeAnchor != null) { // For vertical walls, size.y is the wall height, size.x/z are width/depth Vector3 hitCubeDimensions = planeAnchor.size; Debug.Log($"HitCube Size: X={hitCubeDimensions.x:F2}m, Y={hitCubeDimensions.y:F2}m, Z={hitCubeDimensions.z:F2}m"); } }
Case 2: Custom Spawned HitCube
If you’re instantiating your own Cube prefab at the hit position, you need to account for its scale relative to the detected plane:
- When spawning the cube, match its scale to the plane’s extent (or whatever logic you’re using).
- To get the actual world dimensions, multiply the cube’s local scale by its original prefab size (if your prefab isn’t 1x1x1):
public GameObject hitCubePrefab; // Assign your cube prefab in Inspector private Vector3 originalPrefabSize; void Start() { // Cache the original size of your prefab (before any scaling) originalPrefabSize = hitCubePrefab.transform.localScale; } void SpawnAndGetHitCubeSize(ARHitTestResult hitResult) { GameObject spawnedCube = Instantiate(hitCubePrefab, hitResult.worldTransform.position, hitResult.worldTransform.rotation); // Example: Scale cube to match the detected plane's extent ARPlaneAnchor planeAnchor = hitResult.anchor as ARPlaneAnchor; if (planeAnchor != null) { spawnedCube.transform.localScale = planeAnchor.size; } // Calculate actual dimensions Vector3 actualDimensions = spawnedCube.transform.localScale / originalPrefabSize; Debug.Log($"Custom HitCube Size: {actualDimensions}"); }
Key Pitfalls to Avoid
- Listen for plane updates: ARKit refines plane data over time, so subscribe to
ARSession.OnPlaneAnchorUpdatedto refresh the HitCube size as the plane detection improves. - Hit test type matters:
ExistingPlaneUsingExtentgives you a rectangular bounds, whileExistingPlaneUsingGeometryuses the raw detected shape—stick to Extent if you need a consistent cube. - Axis alignment: For vertical walls, ARKit’s
ARPlaneAnchor.sizeuses Y for height, X/Z for width/depth—make sure your cube’s rotation matches the plane’s orientation to get accurate axis values.
内容的提问来源于stack exchange,提问作者mejume
相关产品推荐
相关产品推荐

