本文主要是介绍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);}
}
这篇关于Unity2D实现鼠标拖动物体移动(简单好抄)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!