[脚本] 改自相机的MouseOrbit脚本,增加了相机距对象的高度,鼠标滚轮操作还有鼠标右键移动相机操作
2012-04-17 08:26
423 查看
using UnityEngine; using System.Collections; [AddComponentMenu("Camera-Control/Mouse OrbitC")] public class MouseOrbitC : MonoBehaviour { // Use this for initialization public Transform target; public float distance = 10.0f;//相机距对象距离 public float height = 10.0f; //相机距对象高度 public float Maxdistance = 40.0f; //相机距对象最大距离 public float Mindistance = 10.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float mousedown_Movespeen = 10.0f; public int yMinLimit = -20; public int yMaxLimit = 80; public float WheelSpeed = 5.0f; //鼠标滚轮控制相机镜头移动速度 private float x = 0.0f; private float y = 0.0f; private float scale = 0.0f; //距离和高度比,当鼠标滚轮操作,距离变化,要据这个值求出相应的高度变化 private bool mousedown = false; //判断鼠标右键是否按下 private float startmouse = 0.0f; //鼠标右键按下时,鼠标的点位置 //private intital intitalScript; //private tankrotation tankrotationScript; //private float wheeeldistance = 0.0f; void Start () { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; scale = distance / height ; // intitalScript = gameObject.GetComponent<intital>(); // tankrotationScript = target.gameObject.GetComponent<tankrotation>(); // Make the rigid body not change rotation if (rigidbody){ rigidbody.freezeRotation = true; } } // Update is called once per frame void Update () { } void LateUpdate(){ if(Input.GetButtonDown("RightMouse")){ //检测是否按下鼠标右键 RightMouse是在InputManager里面设定的,右键值为mouse 1 mousedown = true; Debug.Log("mouseRight"); // intitalScript.enabled = false; // tankrotationScript.enabled = false; // if(startmouse ==0.0f){ // startmouse = Input.mousePosition.x; // } // float mouse_md = Input.mousePosition.x - startmouse; // Debug.Log(mouse_md+""); // //if(mouse_md >=0){ // transform.Rotate(transform.up * mouse_md *Time.deltaTime); // startmouse = Input.mousePosition.x; //} } if(mousedown){ //当鼠标右键被按下 if(startmouse ==0.0f){ //第一次被按下,对鼠标的初始位置进行初始化 startmouse = Input.mousePosition.x; } float mouse_md = Input.mousePosition.x - startmouse; //取得鼠标右键按下时移动的位移值 Debug.Log(mouse_md+""); //if(mouse_md >=0){ transform.RotateAround(target.position,Vector3.up,mouse_md * mousedown_Movespeen * Time.deltaTime);//根据位移值正负和大小对摄相机对行旋转 //transform.Rotate(transform.up * mouse_md * mousedown_Movespeen * Time.deltaTime); startmouse = Input.mousePosition.x;//将鼠标初始位置设为此帧鼠标位置 } if(Input.GetButtonUp("RightMouse")){//判断当鼠标右键松开 mousedown = false; // intitalScript.enabled = true; // tankrotationScript.enabled = true; Debug.Log("hello"); } if (target&&!mousedown) { //当target存在且鼠标没有按住右键 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; distance -= Input.GetAxis("Mouse ScrollWheel") * WheelSpeed; //当滚轮操作,对距离值进行改变 distance = Mathf.Clamp(distance,Mindistance,Maxdistance); height = distance / scale; y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 position = rotation * new Vector3(0.0f, height, -distance) + target.position; transform.rotation = rotation; transform.position = position; } } static float ClampAngle (float angle, float min,float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); } }
相关文章推荐
- [脚本]实现游戏对象跟随鼠标方向移动
- [脚本]实现游戏对象跟随鼠标方向移动
- unity。。。完美简单解决靠鼠标在屏幕上的移动来控制相机水平,垂直旋转的c#脚本
- Unity3D鼠标控制摄像机“左右移动控制视角+WASD键盘控制前后左右+空格键抬升高度”脚本
- python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为
- [脚本]实现游戏对象跟随鼠标方向移动
- 给鼠标右键增加个复制到移动到功能
- 单击操作即将鼠标移动至某个对象
- 鼠标右键新建增加菜单项目
- 安装ATi显卡驱动后增加的鼠标右键菜单的清理
- 在Javascript操作JSON对象,增加 删除 修改的简单实现
- Python脚本控制的WebDriver 常用操作 <十一> 操作测试对象
- iOS UITextView 高度随文字自动增加,并跟随键盘移动(二)
- 分享一个Unity3D点击模型跟随鼠标移动的小脚本(包括屏幕视图到三维空间的坐标转换)
- C# 如何用按钮实现鼠标滚轮操作
- 轻轻松松为Sliverlight控件或页面添加鼠标滚轮操作
- 一个java3d程序,作为熟悉3d技术的基础(可以使用鼠标对场景内的3d对象进行操作)
- 按住鼠标右键,移动场景内镜头(适用于塔防等游戏视角)
- [软件推荐]AutoHotKey-更加快速的操作,使用自己编写脚本让你的鼠标休息下
- JavaScript对象属性检查、增加、删除、访问操作实例