unity3d 鼠标控制物体上下、左右、旋转
2018-04-16 20:41
1776 查看
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));
}
}
} 阅读更多
相关文章推荐
- 【Unity 3D学习】获取鼠标滑动方向(上下左右)-控制摄像机围绕物体旋转
- Unity3d中使用鼠标控制物体的平移旋转缩放
- Unity3d鼠标拖拽控制物体720°旋转
- Unity3D控制物体前后左右移动、旋转及相机跟随
- unity3d控制物品的移动(鼠标或上下左右WASD)
- 控制物体随鼠标旋转
- unity中用鼠标控制物体实现缩放、平移、旋转
- Unity3d通过鼠标缩放查看和旋转物体
- Unity用键盘控制物体左右旋转前后移动的c#脚本
- Unity3D 控制物体移动、旋转、缩放
- Uniyt-UGUI上左右滑动鼠标控制模型旋转
- Unity3D 几个基本动画(控制物体移动、旋转、缩放)
- Unity3D 控制物体移动、旋转、缩放
- unity 用鼠标控制相机绕着物体旋转脚本
- [Unity3d]控制物体的旋转以及缩放
- Unity3d控制物体的旋转以及缩放
- Unity3D【脚本】鼠标对摄像机的控制 - 点击鼠标,摄像机围绕圆心旋转(第三次修改)
- 【Unity3D】使用鼠标键盘控制Camera视角(即时战略类游戏视角):缩近,拉远,旋转
- [Unity3d]控制物体的旋转以及缩放
- unity3d实现控制摄像机放大缩小视野加左右可旋转视角角度