对象拖动不以与指针相同的速度移动

本文关键字:速度 移动 指针 拖动 对象 | 更新日期: 2023-09-27 18:07:35

我正试图实现一个拖放系统,但我有一个问题,目前的拖放率。目前,当我拖动一个对象时,它的移动量只是指针实际移动量的一小部分。我该如何解决这个问题?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class DragHandling : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
{
    public float partScale;
    [HideInInspector] public Transform placeholderParent = null;
    [HideInInspector] public Transform parentToReturnTo = null;
    [HideInInspector] public GameObject trashCan; 
    [HideInInspector] public GameObject partsPanel;
    [HideInInspector] public GameObject partsWindow;
    [HideInInspector] public GameObject buildBoard;
    GameObject placeholder = null;
    GameObject dragLayer;
    Vector3 buildPanelScale;
    Vector3 partsPanelScale = new Vector3(1.0f, 1.0f, 1.0f);
    Vector3 startPosition;
    private Vector3 offset = Vector3.zero;
    void Start ()
    {
        dragLayer = GameObject.FindGameObjectWithTag("DragLayer");
        buildBoard = GameObject.FindGameObjectWithTag("Board");
        partsPanel = GameObject.FindGameObjectWithTag("Parts");
        partsWindow = GameObject.FindGameObjectWithTag("PartsWindow");
        trashCan = GameObject.FindGameObjectWithTag("Trash");
    }
    #region IPointerClickHandler implementation
    public void OnPointerClick (PointerEventData eventData)
    {
        if(transform.parent.gameObject == buildBoard)
            transform.SetAsLastSibling();
    }
    #endregion
    #region IBeginDragHandler implementation
    public void OnBeginDrag (PointerEventData eventData)
    {
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        worldPos.z = transform.position.z;
        offset = worldPos - transform.position;
        // create placeholder gap and hold correct position in layout
        placeholder = new GameObject();
        placeholder.transform.SetParent(transform.parent);
        placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex());
        parentToReturnTo = transform.parent;                                    // store original parent location
        placeholderParent = parentToReturnTo;                                   // set placeholder gameobject transform
        GetComponent<CanvasGroup>().blocksRaycasts = false;                     // turn off image raycasting when dragging image in order to see what's behind the image            
    }
    #endregion
    #region IDragHandler implementation
    public void OnDrag (PointerEventData eventData)
    {
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        worldPos.z = transform.position.z;
        worldPos = worldPos - offset;
        transform.position = worldPos;
        if(transform.parent.gameObject == partsPanel)
            transform.SetParent(dragLayer.transform);                                       // pop object to draglayer to move object out of parts Panel
        if(transform.parent.gameObject == buildBoard)
            transform.SetParent(dragLayer.transform);
    }
    #endregion
    #region IEndDragHandler implementation
    public void OnEndDrag (PointerEventData eventData)
    {
        offset = Vector3.zero;
        transform.SetParent(parentToReturnTo);                                  // Snaps object back to orginal parent if dropped outside of a dropzone
        transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());     // Returns card back to placeholder location
        GetComponent<CanvasGroup>().blocksRaycasts = true;                      // turn Raycast back on
        Destroy(placeholder);                                                   // kill the placeholder if object hits a drop zone or returns to parts panel
        if(transform.parent.gameObject == buildBoard)
        {
            buildPanelScale = new Vector3(partScale, partScale, partScale);
            transform.localScale = buildPanelScale;
            transform.SetAsLastSibling();                                       // always place last piece on top
        }
        if(transform.parent.gameObject == partsPanel)
            transform.localScale = partsPanelScale;
    }
    #endregion
}

对象拖动不以与指针相同的速度移动

就在后面,您应该使用eventData.position而不是Input.mousePositionScreenToWorldPointVector3,而Input.mousePositionVector2。我个人也不会使用ScreenToWorldPoint来达到这个目的,我会使用光线投射,并使用RayCastHit hit.point来放置物体,因为它被拖动,这对你来说会更准确。

实际上,因为你正在使用Unity画布,你应该使用RectTransformUtility.ScreenPointToLocalPointInRectangle(...),并把你想要拖动的父矩形或画布。它会给出你想要的坐标。

实现:

RectTransform parentRect = (RectTransform)objectToBeDragged.transform.parent;
Vector2 posInParent;
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, eventData.position, Camera.main, out posInParent);
objectToBeDragged.localPosition = posInParent;

我也刚刚意识到你可能遇到的另一个问题是你使用的是transform.position而不是transform.localPosition我不能肯定这是问题,但有时这真的会给你带来麻烦。