You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Raycast沿正确方向缩放物体?运行时Box绘制适配问题求助

问题描述

我需要实现运行时创建Box的功能:初始点为首次鼠标点击位置(Box轴心位于左下角),按住鼠标拖动时,根据X、Y方向的距离缩放Box,以此绘制出目标尺寸的Box。但目前该功能仅在一面墙上能正常工作,其余墙面均无法正确缩放,推测这与相机旋转相关,但不确定是否需要对缩放物体应用旋转或其他处理。当前仅测试了4面各旋转90度的墙面,后续还会涉及45度、60度等任意角度的墙面。

代码片段

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;
        }
    }
}
解决方案

问题根源

当前直接使用世界坐标系的X、Y轴差值设置Box的localScale,这种方式仅在墙面与世界坐标系完全对齐时有效。当墙面旋转后,世界坐标的X/Y轴不再对应Box自身局部坐标系的轴向,导致缩放方向与拖动方向不匹配。

修正步骤

  1. 记录初始局部坐标:在首次点击时,将世界空间的点击点转换为Box局部空间的坐标,保存为初始参考点。
  2. 计算局部空间差值:拖动时,将当前世界空间的点击点转换为Box局部空间坐标,与初始局部点做差,得到局部坐标系下的宽高。
  3. 确保缩放为正:因为Box轴心在左下角,需保证缩放值为正,避免出现反向缩放。

修改后的代码

public GameObject currentBox;
private float speed = 4;
private Vector3 initialLocalPoint; // 保存初始点击的局部坐标

private void Update()
{
    // 相机旋转逻辑
    if (Input.GetMouseButton(1))
    {
        transform.eulerAngles += speed * new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
    }
    
    // Box缩放逻辑
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        
        bool hasHit = Physics.Raycast(ray, out var hitObject, float.PositiveInfinity, LayerMask.GetMask("Wall"));
        if (!hasHit) return;

        if (touch.phase == TouchPhase.Began)
        {
            currentBox.transform.position = hitObject.point;
            currentBox.transform.rotation = Quaternion.FromToRotation(Vector3.back, hitObject.normal);
            // 将世界点转换为Box局部空间坐标
            initialLocalPoint = currentBox.transform.InverseTransformPoint(hitObject.point);
        }
        else if (touch.phase == TouchPhase.Moved)
        {
            // 将当前世界点转换为Box局部空间坐标
            Vector3 currentLocalPoint = currentBox.transform.InverseTransformPoint(hitObject.point);
            // 计算局部空间内的宽高差值
            float localWidth = currentLocalPoint.x - initialLocalPoint.x;
            float localHeight = currentLocalPoint.y - initialLocalPoint.y;
            // 取绝对值确保缩放为正,适配轴心在左下角的需求
            currentBox.transform.localScale = new Vector3(Mathf.Abs(localWidth), Mathf.Abs(localHeight), 0.01f);
        }
    }
}

额外说明

  • 使用InverseTransformPoint可以将世界空间的点转换为物体局部空间的点,确保差值计算始终基于Box自身的坐标系,不受墙面旋转影响。
  • 如果需要支持拖动时动态调整Box位置(比如从任意角开始绘制),可以根据局部差值的正负调整Box的position,但当前需求是轴心在左下角,所以取绝对值缩放即可。

内容的提问来源于stack exchange,提问作者Darkmax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 19:55:18