using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeMove : MonoBehaviour
{
//用于 实现可以拖动物体移动
//先写一个方法 将屏幕坐标转换成世界装备
//或表达为 将屏幕空间转化为世界空间
Vector3 MyScreenPointToWorldPoint(Vector3 ScreenPoint,Transform target)
{
//1 得到物体在主相机的xx方向
Vector3 dir = (target.position - Camera.main.transform.position) ;
//2 计算投影 (计算单位向量上的法向量)
Vector3 norVec= Vector3.Project(dir, Camera.main.transform.forward);
//返回世界空间
return Camera.main.ViewportToWorldPoint
(
new Vector3(
ScreenPoint.x/Screen.width,
ScreenPoint.y/Screen.height,
norVec.magnitude
)
);
}
Vector3 startPos;
Vector3 endPos;
Vector3 offset;
private void OnMouseDown()
{
//记录起始位置
//因为我们的物体cube所处的是世界空间 鼠标是屏幕空间
//需要将鼠标的屏幕空间转换成世界空间
Unity 3D 中实现拖拽物体的效果
最新推荐文章于 2025-04-09 15:31:02 发布