Drag Box in a Floor

Hello guys!

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.

Thanks in advance.

Regards.

Juan Manuel Serruya.

I think you need to move it to the RayCastHit’s point.

transform.position = ray.point;

Hello,

First, you will have to evaluate the distance between the camera and the floor (raycasthit)

var v =  touchCamera.ScreenToWorldPoint(Vector3( touch.position.x ,  touch.position.y , hit.distance)); 
yourTransform.position.x = v.x + fOffSetSelectedWallX;
yourTransform.position.z = v.z + fOffSetSelectedWallY;

the two variables fOffSetSelectedWallX and Y are there to manage the fact you don’t “touch” always the center of the object.

Hope this help

Thank you!

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

One more try

This seems, like your drag box has a collider, and this collider is returned by Physics.Raycast.

Indeed, my Drag Box has a Collider, but i don’t understand how that affects my problem!

In your script:

Vector3 temp = new Vector3(Input.touches[i].position.x ,  Input.touches[i].position.y , hit.distance);

“hit.distance” is then distance to drag box collider, but you want distance to floor collider.

If drag box need to have collider, then you should use different layer for it and use layerMask in Physics.Raycast.