简体   繁体   English

如何在正确的方向上使用光线投射缩放 object?

[英]How do I scale an object using raycast in the right direction?

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).嗨,我试图在运行时创建一个框,初始点位于第一次单击鼠标时(我在左下角有一个带有 pivot 的框),然后在我想要创建的方向上拖动到鼠标按住鼠标按钮的框(我只是用 x 和 y 的距离缩放框)。

Like is seeing on the video but for some reason only works on one of the walls, 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 rotation to my scaling object or something else to scale correctly on the four walls.就像在视频上看到的那样,但由于某种原因只能在其中一面墙上工作,我知道这必须与相机的旋转有关,但我不知道是否需要对我的缩放 object 或其他东西以在四个墙上正确缩放。

Note: Here I have only four walls each rotated 90 degrees, but in some cases I can have a wall with other degrees like 45 or 60 to mention some examples.注意:这里我只有四面墙,每面都旋转了 90 度,但在某些情况下,我可以有其他度数的墙,例如 45 或 60 度来举一些例子。

I attach a video on my error and where only works on the first wall but not correctly on the others, and some snippets of my code.我附上了关于我的错误的视频,其中仅在第一面墙上有效,但在其他墙上不正确,以及我的一些代码片段。

Video of the problem问题视频

public GameObject currentBox;
private float speed = 4;

private void Update()
{
    //Rotate with mouse
    if (Input.GetMouseButton(1))
    {
        transform.eulerAngles += speed * new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
    }
    
    //Box 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 hitObject, 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
            currentBox.transform.position = hitObject.point;// + Vector3.back * 0.05f;
            currentBox.transform.rotation = Quaternion.FromToRotation(Vector3.back, hitObject.normal);
            //Debug.Log("Direction: " + (currentBox.transform.position - transform.position));
        }
        //If mouse is move scale the box with the width and height dimensions
        else if (touch.phase == TouchPhase.Moved)
        {
            float width = hitObject.point.x - currentBox.transform.position.x;
            float height = hitObject.point.y - currentBox.transform.position.y;
            currentBox.transform.localScale = new Vector3(width, height, 0.01f);
            //currentBox.transform.position = hitObject.point;
        }
    }
}

In TouchPhase.Began , cache the clicked point and also set a second point equal to that value.TouchPhase.Began中,缓存单击的点并设置第二个点等于该值。

In TouchPhase.Moved , update the second point.TouchPhase.Moved中,更新第二个点。

After either case, set the rotation equal to hitObject's rotation, position as the average of the two values, and scale as the difference of the two values.无论哪种情况,设置旋转等于hitObject的旋转,position作为两个值的平均值,缩放作为两个值的差。

Another issue that you're seeing might be due to you setting the local scale to get your desired width and height, but the actual/effective scale will be determined by that AND the scale of the parent GameObject.您看到的另一个问题可能是由于您设置了本地比例以获得所需的宽度和高度,但实际/有效比例将由该比例和父游戏对象的比例决定。 I can't see how it's set in the hierarchy, so maybe it's not an issue, but I didn't notice your planes all are skewed.我看不出它是如何在层次结构中设置的,所以也许这不是问题,但我没有注意到你的飞机都是倾斜的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM