0

The drag and drop code works just fine until I drag the player object over another object. If I drop the player object over another object the drag and drop no longer works. Can anyone explain why this is happening? I'm placing all of the object in an array so that I can keep track of them easier. This is part of the code that is attached to the main camera and creates the objects:

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

        }
}

This is the code that is attached to the player:

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

1 Answer 1

1

I was able to fix the problem by removing the collider from the dropFab.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.