自由变换相机远近、旋转和平移
来源:互联网 发布:全套画线公式源码 编辑:程序博客网 时间:2024/05/10 18:56
嗯,不好意思,前面的代码有点小问题,特地修改了一下。加了一个如果不需要开始自动播放相机移动时,可以自己给相机添加一个开始位置。如果需要设置//开始的目标点移动。则相机开始位置cameraStartPosition为 new vector3(0,0,0,);有兴趣的朋友可以自己添加代码设置根据相机的远近调节鼠标的灵敏度。
自由变换相机远近、旋转和平移。无聊修改了一下别人的代码,写了这样一个代码,但是不记得原创作者是谁了。using UnityEngine;using System.Collections;/// <summary>/// 自由变换相机远近、旋转和平移/// </summary>public class unirotate: MonoBehaviour {//目标物体public Transform target;//鼠标左键//水平旋转速度 public float horizontalSpeed = 45.0f;//水平旋转加速度public float horizontalSpeedMultify = 0.1f;//垂直旋转速度 public float verticalSpeed = 45.0f;//鼠标右键--相机水平移动距离public float cameraShiftHorizon = 100f;//鼠标右键--相机垂直移动距离 public float cameraShiftVertical = 100f; public bool autoRotate = true;//鼠标滚轮的敏感性public float mouseWheelSensitivity = 160.0f; private float x = 0.0f; private float y = 0.0f; private float _distance = 0.0f;private float _verticalValue=0.0f;private float _horizontalValue=0.0f;private float _deltaTime; private Vector3 _fromPosition; private Quaternion _fromRotation;//开始的目标点移动public Transform ToTarget;//设置相机开始时的位置public Vector3 cameraStartPosition; private bool moveCamera = false; private float _moveCameraBeginTime; private const float movelasttime = 3f;public bool IsSolarSystem = false;bool NotInStartMoveCamera = false;// Use this for initializationvoid Start () {if(IsSolarSystem)MoveCamera0();}// Update is called once per framevoid Update () {if(Input.GetKey(KeyCode.Escape)){Application.Quit();}}void LateUpdate() { _deltaTime = Time.deltaTime; if(moveCamera) { float ratio; ratio = (Time.time - _moveCameraBeginTime) / movelasttime; if(ratio > 1) ratio = 1f; transform.position = Vector3.Lerp(_fromPosition, ToTarget.position, ratio); transform.rotation = Quaternion.Slerp(_fromRotation, ToTarget.rotation, ratio); x = transform.eulerAngles.y; y = transform.eulerAngles.x; _distance = (transform.position - target.position).magnitude; if(Time.time - _moveCameraBeginTime > movelasttime + 0.5f) { x = transform.eulerAngles.y; y = transform.eulerAngles.x; _distance = (transform.position - target.position).magnitude; _horizontalValue = 0f; _verticalValue = 0f; moveCamera = false; NotInStartMoveCamera = true; cameraStartPosition = new Vector3(0, 0, 0); print(moveCamera); } } else { if(Input.GetMouseButton(1)) { _horizontalValue -= _deltaTime * cameraShiftHorizon * Input.GetAxis("Mouse X") * 1; _verticalValue -= _deltaTime * cameraShiftVertical * Input.GetAxis("Mouse Y") * 1; } if(Input.GetKey(KeyCode.J)) { _horizontalValue += _deltaTime * cameraShiftHorizon; } else if(Input.GetKey(KeyCode.L)) { _horizontalValue -= _deltaTime * cameraShiftHorizon; } else if(Input.GetKey(KeyCode.I)) { _verticalValue -= _deltaTime * cameraShiftVertical; } else if(Input.GetKey(KeyCode.K)) { _verticalValue += _deltaTime * cameraShiftVertical; } else if(Input.GetKey(KeyCode.R)) { autoRotate = !autoRotate; } else if(Input.GetAxis("Mouse ScrollWheel") != 0.0f) { _distance -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelSensitivity; } if(autoRotate) x -= horizontalSpeedMultify * horizontalSpeed * _deltaTime; else x -= Input.GetAxis("Horizontal") * horizontalSpeed * _deltaTime; if(NotInStartMoveCamera) y += Input.GetAxis("Vertical") * verticalSpeed * _deltaTime; else y += Input.GetAxis("Vertical") * verticalSpeed; if(Input.GetMouseButton(0)) { if(!autoRotate) x += Input.GetAxis("Mouse X") * horizontalSpeed * _deltaTime; y -= Input.GetAxis("Mouse Y") * verticalSpeed * _deltaTime; } Quaternion rotation = Quaternion.Euler(y, x, 0); Debug.Log(rotation); //transform.position = rotation * new Vector3(_horizontalValue, _verticalValue, -_distance) + target.position; transform.position = Vector3.Lerp(this.transform.position, rotation * new Vector3(_horizontalValue, _verticalValue, -_distance) + cameraStartPosition, Time.deltaTime * 5); transform.rotation = Quaternion.Slerp(this.transform.rotation, rotation, Time.deltaTime * 5); }}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); } void MoveCamera0() {_fromPosition = this.transform.position;_fromRotation = this.transform.rotation;_moveCameraBeginTime = Time.time;moveCamera = true; }}
0 0
- 自由变换相机远近、旋转和平移
- 旋转变换和平移变换
- 【机器人】空间描述和变换:平移、旋转、变换(算子)
- 三维空间中刚体的变换旋转和平移
- 第四节 WebGL中的图形变换:旋转、平移和缩放
- unity 手指旋转物体双手控制相机的远近
- 旋转和画图平移
- view仿射变换 缩放 旋转 平移
- matlab 图像几何变换 平移、旋转、缩放
- 三维空间几何变换原理[平移、旋转、错切]
- OpenGL坐标变换 平移,缩放与旋转
- 三维空间中的几何变换-平移旋转缩放
- matlab 图像几何变换 平移、旋转、缩放
- 计算机图形学 几何图形变换算法(缩放、旋转和平移) C语言编写
- NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)
- 图像处理中的旋转、平移、缩放——仿射变换的原理和优化
- UVA1973*3用七个碎片拼成正方形,正方体的旋转和平移,即矩阵变换
- 矩阵的旋转平移正变换及反变换
- macro
- Visual Studio 中两个窗体(WinForm)之间相互传值的方法
- Nexus5设备ROOT
- mysql 锁优化
- GC是什么?为什么要有GC?
- 自由变换相机远近、旋转和平移
- Schematron
- xcodebuild 使用手册
- 预装软件愁多少,wifi共享精灵你知否?
- 结合HierarchyViewer和APK文件反编译获得APP元素id值
- 第二章 武技阁
- XenServer 6: Storage repository on software RAID
- 什么是真正的C++编程?什么是真正的面向接口编程?
- ubuntu13.10 源码安装apache2,php5,mysql 遇到的困难和解决方法