如何统一实现轴拖动

时间:2019-03-26 21:09:29

标签: c# unity3d

我一直在尝试实现与unity编辑器内置的移动工具相似的移动工具。失败之后,我花了很多时间试图找到一种方法。我想到了这个:

Plane groundPlane = new Plane(Vector3.up, Tracked.position);
Plane rightPlane = new Plane(Vector3.right, Tracked.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    switch (axis)
    {
        case Axis.X:
            if (groundPlane.Raycast(ray, out ent))
            {
                float deltaPosX = ray.GetPoint(ent).x - lastHitPos.x;
                Tracked.position = new Vector3(Tracked.position.x + deltaPosX, Tracked.position.y, Tracked.position.z);
                lastHitPos = ray.GetPoint(ent);
            }
            break;
        case Axis.Y:
            if (rightPlane.Raycast(ray, out ent))
            {
                float deltaPosY = ray.GetPoint(ent).y - lastHitPos.y;
                Tracked.position = new Vector3(Tracked.position.x, Tracked.position.y + deltaPosY, Tracked.position.z);
                lastHitPos = ray.GetPoint(ent);
            }
            break;
        case Axis.Z:
            if (groundPlane.Raycast(ray, out ent))
            {
                float deltaPosZ = ray.GetPoint(ent).z - lastHitPos.z;
                Tracked.position = new Vector3(Tracked.position.x, Tracked.position.y, Tracked.position.z + deltaPosZ);
                lastHitPos = ray.GetPoint(ent);
            }
            break;
    }

固定链接! Unity编辑器:https://youtu.be/I0DAVa7GIeo
我目前所拥有的:https://youtu.be/cTykv-KTNOY

0 个答案:

没有答案