【发布时间】:2019-09-02 08:41:36
【问题描述】:
有什么方法可以用三点放置物体?我在(世界)现实世界中有三个或四个点(xyz 位置)(我知道这些点),我想最好地将我的新对象放置(实例化)到这个坐标内。
我厌倦了正确放置对象,但没有好的结果。
编辑:
A B C D 点将是长方体对象的角(例如;砖块)。 需要找到 CuboidObjectToPlace 的位置、旋转和设置比例,以便将放置砖插入 A B C D 中。
我想创建一个长方体对象(我在资产文件夹中将其创建为预制件)。 我知道现实世界中的三个角落。我在此时创建了名为 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);
}
【问题讨论】:
-
请添加您的代码。您能否进一步解释一下……
place ... bestly在这里是什么意思?查看您所期望的行为的一些屏幕截图可能会有所帮助。 -
我想你想把对象放在 n 个给定点的中心。
public List<Transform> positions; public GameObject obj; 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(obj,pos,Quaternion.identity); } -
@SaraKat 是的,我想将拥有此对象的预制件放在资产中。我已经尝试过这个解决方案,但它没有在中心创建我的对象。需要计算比例和旋转?
-
写
Vector3 center = Vector3.zero; foreach (var trans in positions) { center += trans.position; } center /= positions.Count;比较简单
标签: c# unity3d augmented-reality arkit