unity使用鼠标控制移动、缩放、旋转
来源:互联网 发布:权志龙的淘宝店叫什么 编辑:程序博客网 时间:2024/04/28 13:14
unity使用鼠标控制移动、缩放、旋转
注意:物体必须添加刚体组件!!!!
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Move2 : MonoBehaviour{ ///旋转速度,需要调试 public float xSpeed = 250.0f; public float ySpeed = 130.0f; //限制旋转角度,物体绕x轴旋转限制,不能上下颠倒。 public float yMinLimit = -30; public float yMaxLimit = 30; //物体的旋转角度 public float x = 0.0f; public float y = 0.0f; private Vector3 targetScreenpos;//拖拽物体的屏幕坐标 private Vector3 targetWorldpos;//拖拽物体的世界坐标 private Transform target;//拖拽物体 private Vector3 mouseScreenpos;//鼠标的屏幕坐标 private Vector3 offset;//偏移量 Color oricolor = Color.black;//物体本来的颜色 void Start() { //初始化旋转角度,记录开始时物体的旋转角度。 Vector3 angels = transform.eulerAngles; x = angels.y; y = angels.x; target = transform; } void Update() { if (Input.GetMouseButton(1)) { //鼠标沿x轴的移动乘以系数当作物体绕y轴的旋转角度。 //鼠标沿y轴的移动乘以系数当作物体绕x轴的旋转角度。 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f; } Camera.main.fieldOfView = Camera.main.fieldOfView + Input.GetAxis("Mouse ScrollWheel") * -20; } /* void OnMouseEnter() { //当鼠标在物体上时,改变物体颜色。 GetComponent<Renderer>().material.color = Color.blue; } void OnMouseExit() { GetComponent<Renderer>().material.color = oricolor; }*/ //被移动物体需要添加collider组件,以响应OnMouseDown()函数 //基本思路。当鼠标点击物体时(OnMouseDown(),函数体里面代码只执行一次),记录此时鼠标坐标和物体坐标,并求得差值。如果此后用户仍然按着鼠标左键,那么保持之前的差值不变即可。 //由于物体坐标是世界坐标,鼠标坐标是屏幕坐标,需要进行转换。具体过程如下所示。 IEnumerator OnMouseDown() { targetScreenpos = Camera.main.WorldToScreenPoint(target.position); mouseScreenpos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenpos.z); offset = target.position - Camera.main.ScreenToWorldPoint(mouseScreenpos); while (Input.GetMouseButton(0))//鼠标左键被持续按下。 { mouseScreenpos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenpos.z); targetWorldpos = Camera.main.ScreenToWorldPoint(mouseScreenpos) + offset; target.position = targetWorldpos; yield return new WaitForFixedUpdate(); } } void LateUpdate() { //限制绕x轴的移动。 y = Mathf.Clamp(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, -x, 0); transform.rotation = rotation; }}
阅读全文
0 0
- unity使用鼠标控制移动、缩放、旋转
- unity 鼠标移动 缩放,旋转
- Unity 控制物体移动 旋转 缩放
- Unity 移动端简单手势控制(移动,旋转,缩放)
- OpenGL之鼠标控制坐标系旋转、缩放、移动
- Unity 3D:控制相机旋转、移动、缩放等功能
- Unity鼠标控制物体360°旋转和点击移动
- Unity移动旋转比例缩放
- unity 移动端 缩放 旋转
- Unity控制移动、旋转
- Unity触摸控制旋转缩放
- Unity3d中使用鼠标控制物体的平移旋转缩放
- unity 控制对象移动、旋转
- Unity设置物体移动、缩放和旋转
- Unity 鼠标控制角色移动
- Unity通过射线碰撞选择模型,然后控制模型的移动、旋转、缩放等动作
- unity 手指移动物体旋转两个手指控制物体大小缩放
- unity 控制摄像机旋转和缩放
- input框微信Wep阻止软键盘弹出。
- 虚幻4(兼容4.18)官方文档整理翻译UMG按钮风格
- 习题6.7
- 类:BigDecimal,BigInteger,Math,System
- Kubernetes之kubectl常用命令
- unity使用鼠标控制移动、缩放、旋转
- Qt从指定目录读取图像文件保存并在QLabel上不断显示Demo
- dp经典问题最大子序列和问题
- MVC分层思想
- 数字在排序数组中出现的次数
- 常识性算法一弹
- 欢迎使用CSDN-markdown编辑器
- RecyclerView baseadapter
- 从头开始搭建一个dubbo+zookeeper平台