I’m trying to code a drag and drop code for a Box in a plane (the floor).
I’m currently using this code :
void checkCollision()
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray();
if(Input.touchCount > 0)
{
for(int i = 0; i < Input.touchCount; i++)
{
ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
if(Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
if(Input.touches[i].phase == TouchPhase.Moved || Input.touches[i].phase == TouchPhase.Began)
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (Input.touches[i].position.x, Input.touches[i].position.y, 10.0f));
}
}
}
}
}
This code drag and drops the Box but of course it doesn’t stay in the floor, if i add this line :
transform.position = new Vector3(transform.position.x, 1.0f, transform.position.z);
The Box stays in the floor but doesn’t quite follow my finger (of course this works if the camera it’s in a bird’s view, but if it have some kind of angle, the Box doesn’t follow my finger right, that’s because i’m not applying a transformation to the box , i’m just setting the Y position to 1.0f (right to the floor)).
I am wondering if somebody have experienced with this problem.
I don’t understand how to calculate those offsets (fOffSetSelectedWallX and fOffSetSelectedWallY)…
It’s working better now but it gets sightly away from the touch when it’s close to the borders of the screen, but i’m sure it is because i’m not using the offsets.
this is my current code :
if(Input.touchCount > 0)
{
for(int i = 0; i < Input.touchCount; i++)
{
ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
if(Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
if(Input.touches[i].phase == TouchPhase.Moved || Input.touches[i].phase == TouchPhase.Began)
{
Vector3 temp = new Vector3(Input.touches[i].position.x , Input.touches[i].position.y , hit.distance);
transform.position = (Camera.main.ScreenToWorldPoint(temp));
transform.position = new Vector3(transform.position.x , 1.0f, transform
.position.z);
}
}
}
}