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:
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, theTransformcomponent will show you itsPosition(world space by default) or switch toLocal Positionto see coordinates relative to its parent object.通过代码获取(开发核心场景)
This is what you’ll rely on for dynamic behavior. Here are the most practical approaches:- 直接引用目标对象
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}"); } - 通过对象名称查找
Use this only if you know the exact object name—note it’s not super efficient for complex scenes, so avoid calling it inUpdate():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!"); } } - 通过标签查找
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}"); } } - 通过组件查找
If your target has a specific component (likeMeshRenderer), 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}"); } }
- 直接引用目标对象
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 itstransform.positionlike 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

