放置具有 3-4 个 3D 对象节点(角)的对象(c# unity)



有什么方法可以放置带有三点的对象吗?我在(世界(现实世界中有三个或四个点(xyz 位置((我知道这个点(,我想最好地将我的新对象放置(实例化(到这个坐标内。

我厌倦了正确放置对象,但没有好的结果。

编辑:

A B C D 点将是长方体对象的角(例如;砖块(。 需要找到长方体对象到位置的位置,旋转和设置比例,以便放置砖块插入A B C D。

带 4 角的长方体物体

我想创建一个长方体对象(我在资产文件夹中将其创建为预制件(。 我知道现实世界中的三个角落。我在这一点上创建了名为 leftSphere、rightSphere、topSphere、backSphere 的对象。 如何找到放置长方体预制件的中心(位置(和旋转。

public List<Transform> positions;
public GameObject cubloidObjectPrefabToPlace;
private GameObject topSphere; // A corner of real cuboid object in real world
private GameObject leftSphere; // B corner of real cuboid object in real world
private GameObject rightSphere; // C corner of real cuboid object in real world
private GameObject backSphere; // D corner of real cuboid object in real world

..

void Update(){
findCornersInRealWorld(); // found corners and 
//placed A, B, C, D objects
positions.Add(leftSphere.transform);
positions.Add(rightSphere.transform);
positions.Add(topSphere.transform);
positions.Add(backSphere.transform);

createAtCenter();         // Want to implement here..
}

..

void createAtCenter()
{
float x = 0, y = 0, z = 0;
foreach (Transform p in positions)
{
x += p.position.x;
y += p.position.y;
z += p.position.z;
}
x /= positions.Count;
y /= positions.Count;
z /= positions.Count;
Vector3 pos = new Vector3(x, y, z);
Instantiate(cubloidObjectPrefabToPlace, pos, Quaternion.identity);
}    

所以我在场景中有一个长方体对象,我知道边缘点和角点。我想放置一个长方体对象(在资产中(而不是完全相同的位置、旋转和缩放的旧长方体对象。如何设置对象的比例、旋转和位置

positions.Add(edge1.transform);
positions.Add(edge2.transform);
positions.Add(edge3.transform);
positions.Add(edge4.transform);
positions.Add(edge5.transform);
positions.Add(edge6.transform);
positions.Add(edge7.transform);
positions.Add(edge8.transform);

设置比例?

cubloidObjectPrefabToPlace.transform.localScale = new Vector3(
edge1.transform.localScale.x, 
edge2.transform.localScale.y, 
edge3.transform.localScale.z);

设置位置?

Vector3 center = Vector3.zero;
foreach (var trans in positions)
{
center += trans.position;
}
center /= positions.Count;

旋转??

Instantiate(cubloidObjectPrefabToPlace, center, Quaternion.identity);

仍然不正确的位置、旋转和不正确的刻度

最新更新