比较两个Vector3的

时间:2015-05-06 21:11:57

标签: c# unity3d

我不确定我是否正确比较这些Vector3

我在做什么,是这样的:

HashSet<Vector3> handles = new HashSet<Vector3>();
Vector3[] vertices;
HashSet<Vector3> selectedHandles;

void OnEnable(){
    Creator t = (Creator)target;
    Mesh mesh = t.GetComponent<MeshFilter>().sharedMesh;
    if (mesh != null) {
        vertices = mesh.vertices;
        Vector3 lp = t.transform.position;
        foreach(Vector3 v in vertices){
            Vector3 p = (lp - v);
            handles.Add(new Vector3(p.x, -p.z, p.y));
        }
    }
}

此方法从网格中获取顶点并在选择对象时保存它们(请注意new Vector3(p.x, -p.z, p.y) x和y由于网格导入的方式而反转。这使我可以对齐下一部分代码与网格)。

然后我在鼠标的半径范围内添加了句柄,但为了简单起见,我将在下面删除它。现在,当我点击网格时,我试图将我的selectedHandles与数组中的顶点进行比较,但由于某种原因,我的代码看不到它们。

public void OnSceneGUI(){
    selectedHandles = new HashSet<Vector3>();
    foreach(Vector3 handle in handles){
        Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
        selectedHandles.Add(handle);
    }
    CompareSelected();
}

void CompareSelected(){
    if(Event.current.type == EventType.mouseDown){
        foreach(Vector3 handle in selectedHandles){
            for(int i = 0; i < vertices.Length; i++){
                if(Vector3.Distance(vertices[i], handle) < 0.01f){
                    Debug.Log("Hi");
                }
            }
        }
    }
}

我比较错误?

1 个答案:

答案 0 :(得分:0)

我需要将其转换回原始值,以下是我的做法:

<强> CompareSelected()

Creator t = (Creator)target;
Vector3 lp = t.transform.position;
foreach(Vector3 handle in selectedHandles){
    for(int i = 0; i < vertices.Length; i++){
        Vector3 p = (handle - lp);
        Vector3 h = new Vector3(p.x, -p.z, p.y);
        float dist = Vector3.Distance(vertices[i], h);
        if(dist < 0.01f){
            newV[i] = h + (-t.transform.forward * 0.01f);
        }
    }
}