Hi I was trying to make a box on runtime where the initial point is on the first click of the mouse (I have a box with a pivot on the bottom left corner) and then drag to the mouse in the direction where I want to create a box while holding the mouse button (I just scale the box with the distance in x and y).
Like you can see on the video only works correctly on the back wall, and on the other walls does’t work correctly with mix results, I know that this has to be related with the rotation of the camera, but I don’t know if I need to apply some kind of vector to my object or something else to scale the object correctly on all the walls like the wall that is behind me.
I hope some can guide me in the right direction to solve this problem, thanks in advance
I attach a video on my problem and some snippets of my code.
//Door Scale logic
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Ray ray = Camera.main.ScreenPointToRay(touch.position); //ray from camera
bool hasHit = Physics.Raycast(ray, out var hit, float.PositiveInfinity,LayerMask.GetMask("Wall")); //raycast from camera to infinity
//If doesn't hit with wall exit
if (!hasHit)
return;
//If hits
if (touch.phase == TouchPhase.Began)
{
//Set the pivot of the box on the first hit point
currentDoor.transform.position = hit.point;
currentDoor.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
currentDoor.transform.localScale = Vector3.one * 0.01f;
}
//If mouse is move scale the box with the width and height dimensions
else if (touch.phase == TouchPhase.Moved)
{
float width = 0;
float height = 0;
width = hit.point.x - currentDoor.transform.position.x;
height = hit.point.y - currentDoor.transform.position.y;
currentDoor.transform.localScale = new Vector3(width, height, 0.01f);
}
}