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)
{
return;
}
Debug.Log("Dragging");
//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;
draggingObject.GetComponent<Rigidbody>().MovePosition(hit.point);
}
}
I already have tried the following options:
draggingObject.GetComponent<Rigidbody>().MovePosition(hit.point);
draggingObject.GetComponent<Rigidbody>().MovePosition(hit.normal);
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".