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

Unity中如何获取对象的位置坐标?含现实场景放置3D对象场景

Hey there! Let's tackle your two Unity coordinate questions step by step—these are super common scenarios in both regular and AR development:

1. 在Unity开发环境中获取指定对象的位置坐标

You’ve got two main paths here: quick checks via the editor, or dynamic retrieval with code for runtime logic:

  • 直接在Unity编辑器查看
    If you just need to peek at coordinates during development, simply select the target object in the Hierarchy panel. Over in the Inspector, the Transform component will show you its Position (world space by default) or switch to Local Position to see coordinates relative to its parent object.

  • 通过代码获取(开发核心场景)
    This is what you’ll rely on for dynamic behavior. Here are the most practical approaches:

    1. 直接引用目标对象
      The most efficient method—drag your target object into a public variable in your script, then access its transform directly:
      // Assign your target object in the Inspector first
      public Transform targetObject;
      
      void Update()
      {
          // Get world space coordinates
          Vector3 worldPosition = targetObject.position;
          // Get local coordinates (relative to parent)
          Vector3 localPosition = targetObject.localPosition;
          Debug.Log($"World Position: {worldPosition} | Local Position: {localPosition}");
      }
      
    2. 通过对象名称查找
      Use this only if you know the exact object name—note it’s not super efficient for complex scenes, so avoid calling it in Update():
      void Start()
      {
          GameObject target = GameObject.Find("YourObjectName");
          if (target != null)
          {
              Vector3 pos = target.transform.position;
              Debug.Log($"Found object position: {pos}");
          }
          else
          {
              Debug.LogWarning("Target object not found—double-check the name!");
          }
      }
      
    3. 通过标签查找
      Set a unique tag for your target object first, then use this method for faster, more reliable searches:
      void Start()
      {
          GameObject target = GameObject.FindWithTag("Player"); // Replace with your custom tag
          if (target != null)
          {
              Vector3 pos = target.transform.position;
              Debug.Log($"Tagged object position: {pos}");
          }
      }
      
    4. 通过组件查找
      If your target has a specific component (like MeshRenderer), you can search for that component directly:
      void Start()
      {
          MeshRenderer targetRenderer = FindObjectOfType<MeshRenderer>();
          if (targetRenderer != null)
          {
              Vector3 pos = targetRenderer.transform.position;
              Debug.Log($"Object with MeshRenderer position: {pos}");
          }
      }
      
2. 在Unity中,当3D对象被放置到现实场景后获取位置坐标

This applies to AR scenarios (using tools like AR Foundation, ARKit, or ARCore). The key thing to remember: Unity’s world space aligns with the AR device’s tracking system, so the object’s transform will reflect its real-world position relative to that tracking space.

  • 基础获取方式:直接读取Transform
    Once you’ve placed the object via raycasting or plane detection, just access its transform.position like you would in a regular 3D scene:
    // Example: Instantiate a prefab on a detected AR plane
    public GameObject arPrefab;
    
    void PlaceObjectOnPlane(Pose hitPose)
    {
        GameObject placedObject = Instantiate(arPrefab, hitPose.position, hitPose.rotation);
        // Get the real-world aligned position
        Vector3 realWorldPos = placedObject.transform.position;
        Debug.Log($"Placed object position in real world: {realWorldPos}");
    }
    
  • 更稳定的坐标:绑定到AR锚点
    AR anchors adjust their position as the device’s tracking improves, so binding your object to an anchor will give you more stable coordinates over time:
    public ARAnchor targetAnchor;
    
    void Update()
    {
        if (targetAnchor != null)
        {
            Vector3 stablePos = targetAnchor.transform.position;
            Debug.Log($"Stable anchor-bound position: {stablePos}");
        }
    }
    
  • 获取相对于设备的位置
    If you need the object’s position relative to the device itself, convert the world position using the main camera (which is tied to the device):
    Vector3 worldPos = placedObject.transform.position;
    Vector3 relativeToDevice = Camera.main.transform.InverseTransformPoint(worldPos);
    Debug.Log($"Object position relative to device: {relativeToDevice}");
    }
    

内容的提问来源于stack exchange,提问作者yuuun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 22:57:29