【问题标题】:Unity Drag and Drop not working when objects overlap当对象重叠时,Unity拖放不起作用
【发布时间】:2014-07-23 22:49:33
【问题描述】:

拖放代码可以正常工作,直到我将播放器对象拖到另一个对象上。如果我将播放器对象放在另一个对象上,则拖放不再起作用。谁能解释为什么会这样?我将所有对象放在一个数组中,以便更轻松地跟踪它们。这是附加到主相机并创建对象的代码的一部分:

    public GameObject sphereFab;
    public GameObject dropFab;
    public Vector3 position;
    public static int arraySize; // This variable is set in the class s_initialize
    private Object[] playerHolder;
    private Object[] dropHolder;

    // Use this for initialization
    void Start () {

        // need to send in the size of the array prior to initialization
        playerHolder = new Object[arraySize];
        dropHolder = new Object[arraySize];

        // Create players
        for (int a = 0; a < 12; a+=2) {
            position = new Vector3(a - 5, -3, 0);

            if (a == 0)
                 playerHolder [a] = Instantiate(sphereFab, position, Quaternion.identity);
            else
                playerHolder [a/2] = Instantiate(sphereFab, position, Quaternion.identity);
        }

            // Create drops for players
        for (int a = 0; a < 12; a+=2) {
            position = new Vector3(a - 5, 3, -.1f);

            if (a == 0)
                dropHolder [a] = Instantiate(dropFab, position, Quaternion.identity);
            else
                dropHolder [a/2] = Instantiate(dropFab, position, Quaternion.identity);

        }
}

这是附加到播放器的代码:

private float dist;
    private Vector3 v3Offset;
    private Plane plane;

    void OnMouseDown() {
        plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        float dist;
        plane.Raycast (ray, out dist);
        v3Offset = transform.position - ray.GetPoint (dist);        
    }

    void OnMouseDrag() {
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        float dist;
        plane.Raycast (ray, out dist);
        Vector3 v3Pos = ray.GetPoint (dist);
        transform.position = v3Pos + v3Offset;    
    }
}

【问题讨论】:

    标签: c# user-interface drag-and-drop unity3d


    【解决方案1】:

    我能够通过从 dropFab 中移除碰撞器来解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多