Unity2D实现鼠标拖动物体移动(简单好抄)

1.新建脚本,并将脚本拖到你想要拖动的物体上即可

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class text : MonoBehaviour
{
    private Vector3 offset;
    public int x = 1;
    void OnMouseDown()//鼠标按下
    {
        offset = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    void OnMouseDrag()//鼠标持续按下
    {
        Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
        transform.position = new Vector3(newPosition.x, newPosition.y, transform.position.z);
    }
}

 

相关推荐

  1. Unity 鼠标拖拽3D物体跟随移动的方法

    2024-03-20 09:50:08       38 阅读
  2. Unity2D_角色移动&跳跃

    2024-03-20 09:50:08       53 阅读
  3. unity实现3D物体在UI前方

    2024-03-20 09:50:08       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 09:50:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 09:50:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 09:50:08       82 阅读
  4. Python语言-面向对象

    2024-03-20 09:50:08       91 阅读

热门阅读

  1. 【vscode 常用扩展插件】

    2024-03-20 09:50:08       48 阅读
  2. Mac改造计划

    2024-03-20 09:50:08       42 阅读
  3. 【晴问算法】入门篇—数学问题—西西弗斯串

    2024-03-20 09:50:08       42 阅读
  4. 计算机网络——HTTP

    2024-03-20 09:50:08       39 阅读
  5. Linux小白进阶之路:TOP 10命令详解

    2024-03-20 09:50:08       33 阅读
  6. Webpack的构建流程

    2024-03-20 09:50:08       39 阅读
  7. 算法笔记p414拓扑排序

    2024-03-20 09:50:08       47 阅读
  8. Android 高通平台集成无源码apk示例

    2024-03-20 09:50:08       40 阅读
  9. 网络工程师练习题3

    2024-03-20 09:50:08       46 阅读
  10. 设计模式(结构型设计模式——组合模式)

    2024-03-20 09:50:08       39 阅读