我有两个 3D 对象,最终它们将是不同大小和形状的 3D 模型,但我正在努力使底层功能正确。
一个是常规 Sphere,在位置 (0,-0.5,0) 处有一个名为 s1 的子游戏对象。
一个是按比例缩放 (0.3,0.3,0.3) 的圆柱体,在位置 (0,0.5,0) 的子游戏对象名为 c1。
我正在尝试移动对象,以便两个父对象在同一子对象位置对齐。
下面的代码将气缸位置直接移动到 s1 位置。我只是在计算矢量数学时遇到了麻烦,所以 c1 位置直接在 s1 位置上。我尝试了多种不同的加法、减法、除法来使它们对齐,但没有任何运气。任何帮助理解如何解决这个问题将不胜感激。
public GameObject sphere;
public GameObject cyclinder;
private GameObject s1;
private GameObject c1;
// Start is called before the first frame update
void Start()
{
s1 = sphere.transform.Find("s1").gameObject;
c1 = cyclinder.transform.Find("c1").gameObject;
cyclinder.transform.position = s1.transform.position;
}
虽然您的问题不是很清楚,但我认为您的意思是您试图以这样的方式定位圆柱体和球体,使它们的子对象(s1 和 c1)共享完全相同的世界位置。
我要遵循的步骤是:
例如:
Vector3 finalPos = ... // Position I want both s1 and c1 to share
// Cylinder: world offset, moving from c1 to the parent object
Vector3 cylOffset = cylinder.transform.position - c1.transform.position;
// Position the cylinder, and apply the offset
cylinder.transform.position = finalPos + cylOffset;
// Sphere: world offset, moving from s1 to the parent object
Vector3 sphereOffset = sphere.transform.position - s1.transform.position;
// Position the sphere, and apply the offset
sphere.transform.position = finalPos + sphereOffset;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句