把这个脚本挂载到需要操作的物体上即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragObj : MonoBehaviour
{
    private Vector3 lastMousePosition = Vector3.zero;

    private bool isMouseDown = false;
    void Update()
    {

        if (isMouseDown)
        {
            if (lastMousePosition != Vector3.zero)
            {
                Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
                this.transform.position += offset;
            }
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        }
    }

    public void OnMouseDown()
    {
        isMouseDown = true;
    }

    public void OnMouseUp()
    {
        isMouseDown = false;
        lastMousePosition = Vector3.zero;
    }
}

 

posted on 2020-09-26 19:19  海月CSDN  阅读(1203)  评论(0编辑  收藏  举报