unity鼠标拖动物体旋转
using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 鼠标控制自旋 /// public class SpinWithMouse : MonoBehaviour {private bool isClick = false; private Vector3 nowPos; private Vector3 oldPos; float length = 1; void Start() { AddBoxCollider(); }void OnMouseUp() { //鼠标抬起 isClick = false; }void OnMouseDown() { //鼠标按下isClick = true; }void Update() { nowPos = Input.mousePosition; if (isClick) { //鼠标按下不松手 Vector3 offset = nowPos - oldPos; if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y) && Mathf.Abs(offset.x) > length) { //进行旋转 transform.Rotate(Vector3.up,-offset.x); } } oldPos = Input.mousePosition; }/// /// 添加Collider作为鼠标检测区域 /// public void AddBoxCollider() { BoxCollider box = gameObject.AddComponent(); //参数 box.center = new Vector3(0, 1.2f, 0); box.size = new Vector3(2.4f, 2.4f, 2.4f); } }
本来想用射线检测,发现unity生命周期自己带这个,挺方便的,脚本直接挂在物体上就行
【unity鼠标拖动物体旋转】转载于:https://www.cnblogs.com/sanyejun/p/9118674.html
推荐阅读
- Unity和Android通信系列文章2——扩展UnityPlayerActivity
- unity探究UGUI的Image中sprite和overrideSprite的区别
- unity|unity 在iOS平台跳转appstore
- OpenCV|OpenCV for Unity 通过WebCamTextureToMatHelper帮助类来获取摄像头的画面
- Unity中使用反射机制调用函数
- Unity|Unity 对象池
- 049-python包pyautogui
- 京东退货有感
- IOS打包流程-2打包ipa
- Unity文件下载的内存优化