【问题标题】:Create 3D axis to move object (in Unity)创建 3D 轴以移动对象(在 Unity 中)
【发布时间】:2021-08-08 16:38:11
【问题描述】:

你好!

我想为一个项目制作一个关卡编辑器,为此我需要 3D 轴来移动一个对象。我有axes,但我希望用户能够通过拖动一个轴来移动它们:单击 X 轴(红色),拖动轴并移动(如 unity、blender 或UE 做到了)。

我的问题是我不知道如何让鼠标输入在正确的轴上移动。其实我们可以移动它,但是当你移动X轴时,鼠标不必跟随世界中的X轴而是在屏幕上,所以你只需要从左到右移动鼠标即可。

Blublub。

【问题讨论】:

    标签: unity3d mouse drag axes


    【解决方案1】:

    这是一种方法:

    • 针对 XZ 平面的光线投射
    • 找到从箭头中心开始到命中点结束的方向向量
    • 由于该方向向量可以查看 XZ 平面中的任何方向,因此将其投影到 X 轴上以将其约束到该轴
    • 按结果向量移动箭头
    Vector3 xAxis = Vector3.right;
    Vector3 yAxis = Vector3.up;
    Vector3 arrowsCenterPosition = ...;
    Plane plane = new Plane( yAxis, arrowsCenterPosition ); // Create a virtual plane on XZ directions
    Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    if( plane.Raycast( ray, out float enter ) ) // Raycast against that plane
    {
        Vector3 hitPoint = ray.GetPoint( enter ); // The point that hit the virtual plane
        Vector3 directionToHitPoint = hitPoint - arrowsCenterPosition;
    
        // We want to move in X axis but our directionToHitPoint can be in any direction in XZ plane,
        // so we need to project it onto X axis
        Vector3 xAxisMovement = Vector3.Project( directionToHitPoint, xAxis );
    
        // You can move the arrows by xAxisMovement amount
        arrowsCenterPosition += xAxisMovement;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多