Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 子对象变换位置、旋转、缩放距离原点不正确?_C#_Unity3d_Math_3d_Transform - Fatal编程技术网

C# 子对象变换位置、旋转、缩放距离原点不正确?

C# 子对象变换位置、旋转、缩放距离原点不正确?,c#,unity3d,math,3d,transform,C#,Unity3d,Math,3d,Transform,我们有一个父对象,我们称之为RectangleParent,我们想把子对象,在我们的例子中是Squares,放在这个曲面中(运行时确定的总数在1-10范围内)。两者都位于父对象的边界内,RectangleParent,彼此间隔均匀,从左到右最多两行(最多一行5个项目,最多两行,最多等于10平方米) 编辑开始 结束编辑 统一包装样品 正确示例: 十(10)要放置的对象 五(5)要放置的对象 当前结果 请注意位于原点的父对象(父对象是拉长的矩形,子对象是较小的正方形)。它的子对象按需要放置,

我们有一个父对象,我们称之为
RectangleParent
,我们想把子对象,在我们的例子中是
Squares
,放在这个曲面中(运行时确定的总数在1-10范围内)。两者都位于父对象的边界内,
RectangleParent
,彼此间隔均匀,从左到右最多两行(最多一行5个项目,最多两行,最多等于10平方米)

编辑开始

结束编辑

统一包装样品

正确示例:

十(10)要放置的对象

五(5)要放置的对象

当前结果

请注意位于原点的父对象(父对象是拉长的矩形,子对象是较小的正方形)。它的子对象按需要放置,满足所有要求。放置在父边界内,两行在其父边界内彼此等距排列,并且每个子对象彼此等距排列。如十(10)个对象示例所示

但是请注意,对于不位于原点的父对象-子对象(正方形)不位于其父边界内,两行之间的间距不适合父边界,并且对象不再是正方形(它们现在看起来像矩形)。但是,每行中的子对象彼此之间的间隔是均匀的,因此这里至少满足了一个要求

如果我们指定
Cube.transform
.localPosition而不是
.position

原产地: 行之间的间距仍然正确,但现在每行中的子对象(正方形)之间的间距不正确,并且所有子对象都不在其父对象的边界内

远离原点: 两行对齐正确,但两行之间的间距不正确。这两行不适合其父对象边界。子对象本身有多个问题。它们不是正方形,彼此之间的间距不正确

那么,我们的3d布局数学有什么问题?如何确定对象的位置,以便无论父对象位于何处,子对象都位于其父对象的边界内

代码

public class NewBehaviourScript : MonoBehaviour {

    public int NumberOfObjectsToPlace = 10;   //Place 10 objects
    public int maxRows = 5; // how many objects can be placed in a row
    public int maxColumns = 2; // how many objects can be placed in a column

    public GameObject RectangleParent;  //the parentObject instance
    public GameObject CubePrefab;       //the cube prefab

    // Use this for initialization
    void Start () {
        PlaceObjects();
    }

    // Update is called once per frame
    void Update () {

    }

    void PlaceObjects()
    {
        Vector3 center = RectangleParent.GetComponent<MeshRenderer>().bounds.center;
        Vector3 size = RectangleParent.GetComponent<MeshRenderer>().bounds.size;
        Vector3 corner = -new Vector3(size.x / 2, 0, size.z / 2);
        float stepX = size.x / (maxColumns + 1);
        float stepZ = size.z / (maxRows + 2);
        int placedObjects = NumberOfObjectsToPlace;

        for (int i = 0; i < placedObjects; i++)
        {
            GameObject Cube = Instantiate(CubePrefab);
            Cube.transform.parent = RectangleParent.transform;
            Cube.transform.localRotation = Quaternion.identity;
            Cube.transform.position = corner + new Vector3(stepX, 
                center.y + 0.15f, 
                stepZ * 1.5f + i * stepZ
            );

            if (i == maxRows - 1)
            {
                i -= maxRows;
                placedObjects -= maxRows;
                stepX += size.x / (maxColumns + 1);
            }
        }
    }

}
公共类NewBehaviourScript:MonoBehavior{
public int NumberOfObjectsToPlace=10;//放置10个对象
public int maxRows=5;//一行可以放置多少个对象
public int maxColumns=2;//一列中可以放置多少个对象
public GameObject RectangleParent;//parentObject实例
public GameObject CubePrefab;//多维数据集预置
//用于初始化
无效开始(){
PlaceObjects();
}
//每帧调用一次更新
无效更新(){
}
void PlaceObjects()
{
Vector3 center=RectangleParent.GetComponent().bounds.center;
Vector3 size=RectangleParent.GetComponent().bounds.size;
Vector3角=-新Vector3(大小为.x/2,0,大小为.z/2);
浮动步长x=大小x/(最大列数+1);
浮动步长z=大小.z/(最大行数+2);
int placedObjects=NumberOfObjectsToPlace;
for(int i=0;i

(感谢@Ali Baba的帮助)

您必须使用矩形家长的位置和方向

Cube.transform.position = RectangleParent.transform.position 

                          + corner.x * RectangleParent.transform.right 
                          + corner.y * RectangleParent.transform.up 
                          + corner.z * RectangleParent.transform.forward 

                          + stepX * RectangleParent.transform.right 
                          + (center.y + 0.15f) * RectangleParent.transform.up 
                          + (stepZ * 1.5f + i * stepZ) * RectangleParent.transform.forward;
或者,您应该使用
localPosition
。这里的问题可能是,如果
RectangleParent
的比例不是
1,1,1
,则
localPosition
的值可能会随之缩放,因此您可能需要执行以下操作

Cube.transform.localPosition = new Vector3(
                                   corner.x / RectangleParent.transform.localScale.x, 
                                   corner.y / RectangleParent.transform.localScale.y, 
                                   corner.z / RectangleParent.transform.localScale.z)

                               + new Vector3(
                                   stepX / RectangleParent.transform.localScale.x, 
                                   center.y + 0.15f, 
                                   (stepZ * 1.5f + i * stepZ) / RectangleParent.transform.localScale.z
                               );
Cube.transform.localScale = new Vector3(
                                1 / RectangleParent.transform.localScale.x, 
                                1 / RectangleParent.transform.localScale.y, 
                                1 / RectangleParent.transform.localScale.z
                            );

然后,为了正确重置刻度,您可能需要执行以下操作

Cube.transform.localPosition = new Vector3(
                                   corner.x / RectangleParent.transform.localScale.x, 
                                   corner.y / RectangleParent.transform.localScale.y, 
                                   corner.z / RectangleParent.transform.localScale.z)

                               + new Vector3(
                                   stepX / RectangleParent.transform.localScale.x, 
                                   center.y + 0.15f, 
                                   (stepZ * 1.5f + i * stepZ) / RectangleParent.transform.localScale.z
                               );
Cube.transform.localScale = new Vector3(
                                1 / RectangleParent.transform.localScale.x, 
                                1 / RectangleParent.transform.localScale.y, 
                                1 / RectangleParent.transform.localScale.z
                            );

使用

private void PlaceObjects()
{
var center=RectangleParent.GetComponent().bounds.center;
var size=RectangleParent.GetComponent().bounds.size;
var corner=-新向量3(大小为.x/2,0,大小为.z/2);
var stepX=size.x/(maxColumns+1);
var stepZ=大小.z/(最大行数+2);
var placedObjects=NumberOfObjectsToPlace;
对于(变量i=0;i