1 using UnityEngine; 2 using System.Collections; 3 /// <summary> 4 /// 鼠标位置控制世界空间对象坐标 5 /// 因为只能控制2维坐标,所以要注意相机所看向的方向,及移动对象的坐标轴 6 /// </summary> 7 public class MyTest : MonoBehaviour { 8 9 public Transform Obj;//参考对象 10 private float posX, posY;//获取鼠标2维坐标 11 private Vector3 world;//转换的世界坐标 12 13 void Update() 14 { 15 if (Input.GetMouseButton(0)) 16 { 17 posX = Input.mousePosition.x; 18 posY = Input.mousePosition.y; 19 20 world = Camera.main.ScreenToWorldPoint(new Vector3(posX, posY, 10)); 21 Debug.Log(world); 22 23 Obj.position = world; 24 } 25 } 26 }
时间: 2024-11-09 00:48:08