【问题标题】:Get difference between rotations and positions获取旋转和位置之间的差异
【发布时间】:2019-03-13 21:42:20
【问题描述】:

我在世界空间中有两个对象。 一种是多维数据集没有父级。 第二个是三角形,它有一个父级。 我改变了立方体的位置和旋转。 现在我需要将立方体放在它的第一个位置,但将父级(本地)中的三角形移动到这个位置以适应相同的位置,就好像立方体不会放在前一个位置一样。

【问题讨论】:

  • 你试过什么??我确定你知道如何旋转/移动项目

标签: unity3d


【解决方案1】:
  1. Somwhere 存储cube的原始位置和旋转

    Vector3 origPosition = cube.transform.position;
    Quaternion origRotation = cube.transform.rotation;
    
  2. 获取立方体和三角形之间的偏移值

    Vector3 posOffset = triangle.transform.position - cube.transform.position;
    Quaternion rotOffset = Quaternion.Inverse(cube.transform.rotation) * triangle.transform.rotation;
    
  3. (重新)设置立方体和三角形

    cube.transform.position = origPosition;
    cube.transform.rotation = origRotation;
    
    triangle.transform.position = origPosition + posOffset;
    triangle.transform.rotation = origRotation * rotOffset;
    

例子

public class CubeMover : MonoBehaviour
{
    public Transform cube;
    public Transform triangle;

    private Vector3 origPosition;
    private Quaternion origRotation;

    // Start is called before the first frame update
    private void Start()
    {
        origPosition = cube.transform.position;
        origRotation = cube.transform.rotation;
    }

    [ContextMenu("Test")]
    public void ResetCube()
    {
        Vector3 posOffset = triangle.transform.position - cube.transform.position;
        Quaternion rotOffset = Quaternion.Inverse(cube.transform.rotation) * triangle.transform.rotation;


        cube.transform.position = origPosition;
        cube.transform.rotation = origRotation;

        triangle.transform.position = origPosition + posOffset;
        triangle.transform.rotation = origRotation * rotOffset;
    }
}

(没有三角形,所以我使用了圆柱体......我希望这对你没问题^^)

【讨论】:

  • 这是一个很好的答案,因为它有一些人们在尝试计算偏移量时搜索的东西 - 这真的应该有更多的支持!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2014-04-05
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多