C# 如何使用“OnMouseDrag”在屏幕上拖动对象?

C# 如何使用“OnMouseDrag”在屏幕上拖动对象?,c#,unity3d,C#,Unity3d,试过做2D和3D。我希望玩家点击并拖动对象来堆叠它们。我试着从视频中复制和粘贴代码,但仍然不起作用 更新!现在它有点工作了,但它将物体传送到远离屏幕的地方。我希望玩家能够顺利地点击和拖动对象 截至目前的代码: using UnityEngine; using System.Collections; public class DragObject : MonoBehaviour { private float deltax, deltay; private void OnMo

试过做2D和3D。我希望玩家点击并拖动对象来堆叠它们。我试着从视频中复制和粘贴代码,但仍然不起作用

更新!现在它有点工作了,但它将物体传送到远离屏幕的地方。我希望玩家能够顺利地点击和拖动对象

截至目前的代码:

using UnityEngine;
using System.Collections;

public class DragObject : MonoBehaviour
{
    private float deltax, deltay;


    private void OnMouseDown()
    {
        deltax = 1f;//Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
        deltay = 1f; //Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
    }

    private void OnMouseDrag()
    {
        Vector2 currentTouchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector2((Input.mousePosition).x - deltax, (Input.mousePosition.y) - deltay);
    }
}

现在还不清楚你到底遇到了什么问题,你显然需要提供更多的信息,更好地描述你的问题。但我会尽力帮忙的

要获取鼠标位置,您需要使用:

Input.mousePosition
如果需要鼠标在二维坐标中的位置,可以使用:

Vector2 currentTouchPos = mainCamera.ScreenToWorldPoint(Input.mousePosition);

无论摄影机类型或摄影机在帧之间的移动方式如何,此方法都有效

首先,当第一次单击对象时,获取与摄影机方向垂直的对象位置处的平面:

Plane dragPlane = new Plane(Camera.main.transform.forward, transform.position);
然后,从鼠标所在的位置将光线从相机中取出:

Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
使用以下命令找出光线相交的位置:

这给了你一个在世界空间中的位置,在那里你可以考虑当前的手指。把物体放在那里:

transform.position = fingerPosition;
总的来说,它可能是这样的:

void OnMouseDrag()
{
    Plane dragPlane = new Plane(Camera.main.transform.forward, transform.position);
    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    float enter = 0.0f;
    if (dragPlane.Raycast(camRay, out enter)) 
    {
        Vector3 fingerPosition = camRay.GetPoint(enter);
        transform.position = fingerPosition;
    }
}
无需在MouseDown上执行

另一种方法是跟踪
fingerPosition
如何随时间变化,以及基于此的变换。尽可能多地使用世界单位进行数学运算,这样做很容易:

Vector3 prevFingerPosition = Vector3.zero;

// Same code as before but we use it in different places so it goes in its own method.
// If the Raycast fails, return a decent guess.
private Vector3 GetMouseOnPositionInWorldSpace() 
{
    Plane dragPlane = new Plane(Camera.main.transform.forward, transform.position);
    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    float enter = 0.0f;
    if (dragPlane.Raycast(camRay, out enter))
    {
        return camRay.GetPoint(enter);
    }

    return prevFingerPosition;
}

void OnMouseDown()
{
    prevFingerPosition = GetMouseOnPositionInWorldSpace();
}

void OnMouseDrag()
{
    Vector3 fingerPosition = GetMouseOnPositionInWorldSpace();
    transform.Translate(fingerPosition-prevFingerPosition);
    prevFingerPosition = fingerPosition;
}
使用
OnMouseDrag
处理拖动对象的问题在于,如果光标移动过快,它可能会在单个帧的范围内移出对象,并将对象“放下”


如果不需要这样做,您可能希望在
OnMouseDown
中将
bool
设置为
true
,记住对象已被单击,并将逻辑从
OnMouseDrag
移动到
Update
中,如果
bool
true
,则可以在该位置执行操作。当鼠标松开时,将
bool
设置回
false

Hi games247!欢迎来到StackOverflow。你知道吗,我想我们今天在StackOverflow没有人带着水晶球吧?想进一步澄清一下你在说什么吗?显示您的代码,显示您的错误消息。我能感觉到他们在路上投了一些反对票。按照以下指导原则编辑您的问题:您找到解决方案了吗?@games247设置对象位置时,您仍然没有使用
currentTouchPos
Input.mousePosition
采用像素坐标,因此可能太大了。尝试
transform.position=currentTouchPos-新矢量2(deltax,deltay)
如果Asker使用的是透视相机,这将不起作用,因为
currentTouchPos
将始终是相机的x和y位置。
Vector3 prevFingerPosition = Vector3.zero;

// Same code as before but we use it in different places so it goes in its own method.
// If the Raycast fails, return a decent guess.
private Vector3 GetMouseOnPositionInWorldSpace() 
{
    Plane dragPlane = new Plane(Camera.main.transform.forward, transform.position);
    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    float enter = 0.0f;
    if (dragPlane.Raycast(camRay, out enter))
    {
        return camRay.GetPoint(enter);
    }

    return prevFingerPosition;
}

void OnMouseDown()
{
    prevFingerPosition = GetMouseOnPositionInWorldSpace();
}

void OnMouseDrag()
{
    Vector3 fingerPosition = GetMouseOnPositionInWorldSpace();
    transform.Translate(fingerPosition-prevFingerPosition);
    prevFingerPosition = fingerPosition;
}