转换Unity坐标

时间:2019-06-13 22:49:11

标签: unity3d coordinate-transformation

我在Unity的A,B,C有三个对象。我想将其坐标转换为一个新的坐标系,该坐标系具有将点A作为新原点(0,0,0),将AB作为沿新x轴的线,将AC作为沿新y轴的线。如何在此新坐标空间中获得A,B,C的新坐标?

我查看了transform.InverseTransformPoint(),但不确定如何使它在这里工作。

2 个答案:

答案 0 :(得分:2)

最简单的-非数学-解决方案将是

  1. 将对象A,B,C放在同一父对象下
  2. 平移和旋转父对象而不是单独旋转所有对象=>让Unity为您做数学
  3. (可选)完成后将它们再次放置在以前的位置-或者,如果您仍然希望更改它们的localPosition和localRotation,请将它们保留在旋转后的父对象下。

否则使用例如Transform.TransformPoint,无需育儿

我都将其添加到示例管理器中

public class CoordinateManager : MonoBehaviour
{
    [Header("References")]
    public Transform objectA;
    public Transform objectB;
    public Transform objectC;

    [Header("Coordinate-System Settings")]
    public Vector3 A;
    public Vector3 AB;
    public Vector3 AC;

    [ContextMenu("Apply New Coordinates")]
    public void ApplyNewCoordinates()
    {
        // just for making sure this transform is reset
        transform.position = Vector3.zero;
        transform.rotation = Quaternion.identity;
        transform.localScale = Vector3.one;

        // Make this object parent of objectA,objectB and objectC keeping their current transforms
        // For reverting it later store current parents
        var parentA = objectA.parent;
        var parentB = objectB.parent;
        var parentC = objectC.parent;

        objectA.SetParent(transform);
        objectB.SetParent(transform);
        objectC.SetParent(transform);

        // place this object to the new pivot point A
        // and rotate it to the correct axis
        // so that its right (X) vector euqals AB
        // and its up (Y) vector equals AC
        // Unity will handle the rotation accordingly
        transform.position = A;
        transform.right = AB;
        transform.up = AC;

        // Optionally reset the objects to the old parents
        objectA.SetParent(parentA);
        objectB.SetParent(parentB);
        objectC.SetParent(parentC);
    }

    // Use this method to place another object in the coordinate system of this object
    // without any parenting
    public void SetPosition(Transform obj, Vector3 relativePosition)
    {
        // sets the obj to relativePosition in the 
        // local coordinate system of this rotated and translated manager
        obj.position = transform.TransformPoint(relativePosition);

        // adjust the rotation
        // Quaternions are added by multiplying them
        // so first we want the changed coordinate system's rotation
        // then add the rotation it had before
        obj.rotation = transform.rotation * obj.rotation;
    } 

    // Only for visualization of the pivot point A and the 
    // AB(red) and AC(green) axis in the SceneView
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.white;
        Gizmos.DrawWireSphere(A, 0.1f);

        Gizmos.color = Color.red;
        Gizmos.DrawLine(A, A + AB);

        Gizmos.color = Color.green;
        Gizmos.DrawLine(A, A + AC);
    }
}

enter image description here

答案 1 :(得分:0)

您似乎真正需要知道的唯一值是从A到B的距离以及从A到C的距离。

A' = { 0, 0, 0 }
B' = { (B-A).magnitude, 0, 0 }
C' = { 0, (C-A).magnitude, 0 }