简体   繁体   English

Object 仅在拖动方向上移动少量[Unity 2D isometric]

[英]Object only moving small amount in dragged direction [Unity 2D isometric]

So I'm currently working on an isometric 2D game and I'm trying to drag objects with the mouse.所以我目前正在开发一个等距的 2D 游戏,我正在尝试用鼠标拖动对象。 I've added the script below after following some tutorials but the object only moves a little bit in the direction it's being dragged.在遵循一些教程之后,我添加了下面的脚本,但是 object 只在它被拖动的方向上移动了一点。 I have no idea why the object is not just following the mouse's coordinates but if you need any other info I don't mind providing.我不知道为什么 object 不仅遵循鼠标的坐标,而且如果您需要任何其他信息,我不介意提供。

private void OnMouseDown()
{
    mouseOffset = MouseWorldPos() - (Vector2)transform.position;
}

private void OnMouseDrag()
{
    transform.position = MouseWorldPos() - mouseOffset;
}

private Vector2 MouseWorldPos()
{
    Vector2 mousePos = Input.mousePosition;
    return Camera.main.ScreenToViewportPoint(mousePos);
}

why do you convert to Viewport coordinates?为什么要转换为Viewport坐标?

Viewport space is normalized and relative to the camera.视口空间是标准化的并且相对于相机。 The bottom-left of the camera is (0,0);相机的左下角是(0,0); the top-right is (1,1).右上角是 (1,1)。 The z position is in world units from the camera. z position 采用相机的世界单位。

=> You first do => 你先做

mouseOffset = MouseWorldPos() - (Vector2)transform.position;

which can maximum be a position 0 to √(2) away from the transform.position .最大可以是 position 0√(2)远离transform.position

Then you do然后你做

transform.position = MouseWorldPos() - mouseOffset;

which basically keeps it at your original position +- √(2) in any direction.它基本上保持在你原来的 position +- √(2)在任何方向。


You probably would rather want to do something like eg您可能更愿意做类似的事情,例如

private readonly struct DragInfo
{
    public Plane Plane { get; }
    public Vector3 StartHitPoint { get; }
    public Vector3 OriginalPosition { get; }

    public DragInfo(Plane plane, Vector3 startHitPoint, Vector3 originalPosition)
    {
        Plane = plane;
        StartHitPoint = startHitPoint;
        OriginalPosition = originalPosition;
    }
}

private DragInfo? dragInfo;

private Camera mainCamera;

private void OnMouseDown()
{
    if(!mainCamera) mainCamera = Camera.main;

    var ray = mainCamera.ScreenPointToRay(Input.mousePosition);

    var plane = new Plane(-mainCamera.transform.forward, transform.position);
    if(!plane.Raycast(ray, out var hitDistance)) return;
    
    var startPoint = ray.GetPoint(hitDistance);

    dragInfo = new DragInfo(Plane, startPoint, transform.position);
}

private void OnMouseDrag()
{
    if(!dragInfo.HasValue) return;

    var ray = mainCamera.ScreenPointToRay(Input.mousePosition);

    if(!dragInfo.Plane.Raycast(ray, out var hitDistance)) return;

    var currentPoint = ray.GetPoint(hitDistance);

    var delta = currentPoint - dragInfo.StartHitPoint;
    transform.position = dragInfo.OriginalPosition + delta;
}

private void OnMouseUp()
{
    dragInfo = null;
}

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

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