so I'm working on a mobile game where I have touch input from a Asset from the asset store. It all works perfectly fine, except for one thing: I want to implement a "drag and drop" feature so when I pick up an Item it should follow to the position of my finger. But because the playground is a sphere, I have to do the movement with a Raycast. So my plan is that the item should follow the Raycasts position. My current attempt looks like this:

public void DragTo(float screenX, float screenY)
    if (draggingObject == null)


    //Converts the screen space to world space
    Vector3 mousePosFar = new Vector3(screenX, screenY,      Camera.main.farClipPlane);
    Vector3 mousePosNear = new Vector3(screenX, screenY, Camera.main.nearClipPlane);

    Vector3 mousePosF = mainCam.ScreenToWorldPoint(mousePosFar);
    Vector3 mousePosN = mainCam.ScreenToWorldPoint(mousePosNear);

    //shoots an ray to the world space 
    RaycastHit hit;

    if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit))
        float step = speed * Time.deltaTime;

I already have tried the following options:

draggingObject.transform.position = hit.point;
draggingObject.transform.position = normal;

They all work, but they all have really buggy and strange behaviors. Also when I dont freeze the z. axis on the Rigidbody the item just "flies" up on the z axis, until it reaches the camera. Then it teleportes again down and it all starts again. So its flying up on the ray. So if you have any Ideas why this happens let me know. Any help is appreciated. (Also dont forget that its all happens on a sphere)

UPDATE: It works now. When the dragging began i changed the layer from the object to "Ignore Raycast" and when it end changed it back to "Default".


1 Answer 1


It sounds like your raycast is hitting the draggingObject you are trying to place. You need to ignore your controlled draggingObject while ray-casting.

  • 1
    \$\begingroup\$ You can simplify this further by using Collider.Raycast to cast solely against the constraint sphere and no other colliders. This saves you from devoting a physics layer for this purpose. \$\endgroup\$
    – DMGregory
    Commented Aug 14, 2017 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .