嗯,不好意思,前面的代码有点小问题,特地修改了一下。加了一个如果不需要开始自动播放相机移动时,可以自己给相机添加一个开始位置。如果需要设置有兴趣的朋友可以自己添加代码设置根据相机的远近调节鼠标的灵敏度。//开始的目标点移动。则相机开始位置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 initialization void Start () { if(IsSolarSystem) MoveCamera0(); } // Update is called once per frame void 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; } }
自由变换相机远近、旋转和平移
最新推荐文章于 2024-03-23 20:05:24 发布