You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取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:

  1. Make sure your hit test targets the right plane type. Use ARHitTestResultType.ExistingPlaneUsingExtent for consistent, bounded plane data (this is better than ExistingPlaneUsingGeometry if you need a defined cube size).
  2. Extract the anchor from your hit result and access its size property:
// 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:

  1. When spawning the cube, match its scale to the plane’s extent (or whatever logic you’re using).
  2. 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.OnPlaneAnchorUpdated to refresh the HitCube size as the plane detection improves.
  • Hit test type matters: ExistingPlaneUsingExtent gives you a rectangular bounds, while ExistingPlaneUsingGeometry uses the raw detected shape—stick to Extent if you need a consistent cube.
  • Axis alignment: For vertical walls, ARKit’s ARPlaneAnchor.size uses 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:24:07