在Unity 2D中拖动对象 [英] Drag object in Unity 2D

查看:242
本文介绍了在Unity 2D中拖动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找Unity 2D的对象拖动脚本.我在互联网上找到了一种很好的方法,但是似乎它只在Unity 3D中工作.这对我不利,因为我正在制作2D游戏,并且不会以这种方式与墙"碰撞.

I have looked for an object dragging script for Unity 2D. I have found a good method on the internet, but it seems it's just working in Unity 3D. It's not good for me as I'm making a 2D game and it's not colliding with the "walls" in that way.

我试图将其重写为2D,但是使用Vectors时我陷入了错误.

I have tried to rewrite it to 2D, but I have crashed into errors, with Vectors.

如果您能帮助我将其重写为2D,我将非常高兴.

I would be very glad if you could help me rewrite it to 2D.

以下是在3D模式下工作的代码:

Here's the code what's working in 3D:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]

public class Drag : MonoBehaviour {
    private Vector3 screenPoint;
    private Vector3 offset;

void OnMouseDown() {

    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    transform.position = curPosition;
}
}

推荐答案

对于那些对此代码有疑问的人,我使用ScreenPointToRay使用代数射线广播(快速)来确定应将物体放置在距相机多远的位置.这在正交摄影机和透视摄影机中都可以使用

For the ones who have problem using this code, I used ScreenPointToRay to use algebraic raycasts (fast) to determine how far the object should be placed from the camera. This works in both orthographic and perspective cameras

此外,对象可以使用Collider进行拖动.因此,使用[RequireComponent(typeof(BoxCollider2D))]没有任何意义.

Also, the object could use Collider to able to be dragged around. So there's no point using [RequireComponent(typeof(BoxCollider2D))].

最适合我的最终代码是:

The final code which worked fine for me is:

using UnityEngine;
using System.Collections;

public class DragDrop : MonoBehaviour {
    // The plane the object is currently being dragged on
    private Plane dragPlane;

    // The difference between where the mouse is on the drag plane and 
    // where the origin of the object is on the drag plane
    private Vector3 offset;

    private Camera myMainCamera; 

    void Start()
    {
        myMainCamera = Camera.main; // Camera.main is expensive ; cache it here
    }

    void OnMouseDown()
    {
        dragPlane = new Plane(myMainCamera.transform.forward, transform.position); 
        Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition); 

        float planeDist;
        dragPlane.Raycast(camRay, out planeDist); 
        offset = transform.position - camRay.GetPoint(planeDist);
    }

    void OnMouseDrag()
    {   
        Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition); 

        float planeDist;
        dragPlane.Raycast(camRay, out planeDist);
        transform.position = camRay.GetPoint(planeDist) + offset;
    }
}

这篇关于在Unity 2D中拖动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆