using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
public GameObject cube; //要拖拽的物体
Vector3 mouse; //鼠标
Vector3 screeenV; //存储cube的屏幕坐标
Vector3 world; //记录鼠标坐标转成的世界坐标
void Update()
{
screeenV = Camera.main.WorldToScreenPoint(cube.transform.position);
//当鼠标第一次单击时记录下cube在场景中的坐标,并把世界坐标转成屏幕坐标
mouse = Input.mousePosition; //当鼠标移动时记录下鼠标的坐标
mouse.z = screeenV.z; //因为鼠标的z坐标为0,所以需要一个z坐标
//把鼠标的屏幕坐标转换成世界坐标
world = Camera.main.ScreenToWorldPoint(mouse);
//当鼠标移动时,cube也发生移动,为了让cube的y轴不发生移动,设y轴为原来的y轴
if (Input.GetMouseButton(0))
{
cube.transform.position = new Vector3(world.x, cube.transform.position.y, world.z);
print(cube.transform.position);
}
if (Input.GetMouseButton(1))
{
cube.transform.position = new Vector3(world.x, world.y, cube.transform.position.z);
print(cube.transform.position);
}
if (Input.GetMouseButton(2))
{
cube.gameObject.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") *
Time.deltaTime * 300, -Input.GetAxis("Mouse X") * Time.deltaTime * 300, 0));
}
}
}
using System.Collections;
public class Rotate : MonoBehaviour {
public GameObject cube; //要拖拽的物体
Vector3 mouse; //鼠标
Vector3 screeenV; //存储cube的屏幕坐标
Vector3 world; //记录鼠标坐标转成的世界坐标
void Update()
{
screeenV = Camera.main.WorldToScreenPoint(cube.transform.position);
//当鼠标第一次单击时记录下cube在场景中的坐标,并把世界坐标转成屏幕坐标
mouse = Input.mousePosition; //当鼠标移动时记录下鼠标的坐标
mouse.z = screeenV.z; //因为鼠标的z坐标为0,所以需要一个z坐标
//把鼠标的屏幕坐标转换成世界坐标
world = Camera.main.ScreenToWorldPoint(mouse);
//当鼠标移动时,cube也发生移动,为了让cube的y轴不发生移动,设y轴为原来的y轴
if (Input.GetMouseButton(0))
{
cube.transform.position = new Vector3(world.x, cube.transform.position.y, world.z);
print(cube.transform.position);
}
if (Input.GetMouseButton(1))
{
cube.transform.position = new Vector3(world.x, world.y, cube.transform.position.z);
print(cube.transform.position);
}
if (Input.GetMouseButton(2))
{
cube.gameObject.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") *
Time.deltaTime * 300, -Input.GetAxis("Mouse X") * Time.deltaTime * 300, 0));
}
}
}