【发布时间】:2019-03-13 21:42:20
【问题描述】:
我在世界空间中有两个对象。 一种是多维数据集没有父级。 第二个是三角形,它有一个父级。 我改变了立方体的位置和旋转。 现在我需要将立方体放在它的第一个位置,但将父级(本地)中的三角形移动到这个位置以适应相同的位置,就好像立方体不会放在前一个位置一样。
【问题讨论】:
-
你试过什么??我确定你知道如何旋转/移动项目
标签: unity3d
我在世界空间中有两个对象。 一种是多维数据集没有父级。 第二个是三角形,它有一个父级。 我改变了立方体的位置和旋转。 现在我需要将立方体放在它的第一个位置,但将父级(本地)中的三角形移动到这个位置以适应相同的位置,就好像立方体不会放在前一个位置一样。
【问题讨论】:
标签: unity3d
Somwhere 存储cube的原始位置和旋转
Vector3 origPosition = cube.transform.position;
Quaternion origRotation = cube.transform.rotation;
获取立方体和三角形之间的偏移值
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;
例子
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;
}
}
(没有三角形,所以我使用了圆柱体......我希望这对你没问题^^)
【讨论】: