【问题标题】:Placing an object with 3-4 node(corner) of 3D object (c# unity)用 3D 对象的 3-4 个节点(角)放置一个对象(c# unity)
【发布时间】:2019-09-02 08:41:36
【问题描述】:

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

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

编辑:

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

cuboid object with 4 corner

我想创建一个长方体对象(我在资产文件夹中将其创建为预制件)。 我知道现实世界中的三个角落。我在此时创建了名为 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&lt;Transform&gt; 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


【解决方案1】:

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

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);

仍然不正确的位置、旋转和不正确的比例

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 2018-02-06
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多