unity 控制摄像机旋转和缩放
来源:互联网 发布:linux 完全卸载svn 编辑:程序博客网 时间:2024/04/27 22:54
在做项目的时候,有用到相机绕着某个物体旋转和近距离查看的功能。所以过来贴贴代码。
/// <summary>/// Mouse orbit./// This script use to control a main camera/// </summary>using UnityEngine;using System.Collections;public class MouseOrbit : MonoBehaviour{ public GameObject target; //a target look at public float xSpeed; //speed pan x public float ySpeed; //speed pan y public float yMinLimit; //y min limit public float yMaxLimit; //y max limit public float scrollSpeed; //scroll speed public float zoomMin; //zoom min public float zoomMax; //zoom max private float distance; private float distanceLerp; private Vector3 position; private bool isActivated; private float x; private float y; // Use this for initialization void Start() { //Warning when not found target if (target == null) { target = GameObject.FindGameObjectWithTag("Player"); if (target == null) { Debug.LogWarning("Don't found player tag please change player tag to Player"); } } //Setup Pos Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; CalDistance(); } void LateUpdate() { ScrollMouse(); RotateCamera(); } //Roate camera method void RotateCamera() { if (Input.GetMouseButtonDown(1)) { isActivated = true; } if (Input.GetMouseButtonUp(1)) { isActivated = false; } if (isActivated) { y -= Input.GetAxis("Mouse Y") * ySpeed; x += Input.GetAxis("Mouse X") * xSpeed; y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 calPos = new Vector3(0, 0, -distanceLerp); position = rotation * calPos + target.transform.position; transform.rotation = rotation; transform.position = position; } } //Calculate Distance Method void CalDistance() { distance = zoomMax; distanceLerp = distance; Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 calPos = new Vector3(0, 0, -distanceLerp); position = rotation * calPos + target.transform.position; transform.rotation = rotation; transform.position = position; } //Scroll Mouse Method void ScrollMouse() { distanceLerp = Mathf.Lerp(distanceLerp, distance, Time.deltaTime * 5); if (Input.GetAxis("Mouse ScrollWheel") != 0) { // get the distance between camera and target distance = Vector3.Distance(transform.position, target.transform.position); distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed, zoomMin, zoomMax); } } //Scroll Limit Method float ScrollLimit(float dist, float min, float max) { if (dist < min) dist = min; if (dist > max) dist = max; return dist; } //Clamp Angle Method float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); }}
这就是全部的代码,target是我们要围绕的目标对象。
0 0
- unity 控制摄像机旋转和缩放
- unity旋转 缩放摄像机
- Unity滚轮控制UI和摄像机缩放冲突问题
- Unity物体或摄像机的旋转缩放
- Unity触摸控制旋转缩放
- unity重力感应控制摄像机旋转
- unity 鼠标控制摄像机围绕物体旋转
- Unity 控制摄像机旋转、放大、缩小
- Unity中的旋转和缩放
- Unity 控制物体移动 旋转 缩放
- unity使用鼠标控制移动、缩放、旋转
- Unity3D入门(五):鼠标控制旋转、摄像机缩放
- Unity3D入门(五):鼠标控制旋转、摄像机缩放
- 用C#控制摄像机的移动 旋转 缩放
- Unity3D开发之控制摄像机围绕物体旋转 平移 缩放
- Unity摄像机旋转和位移跟随
- Unity摄像机旋转和位移跟随
- unity GearVr 摄像机移动和旋转问题
- 安卓杂记(六)仿“迷你飞信”更改用户头像
- angularjs 点击div外事件
- cookie 和 session
- System.Diagnostics命名空间里的Debug类和Trace
- 使用xctool 打包 , 到 越狱机 免除codesign ,到年末寄语
- unity 控制摄像机旋转和缩放
- 查看oracle是否锁表
- Inside SQLite 第二章 Database File Format数据库文件格式
- 【SzNOI语法百题】【d049】字符三角形
- 外链测试
- LINUX kernel memory barrier summary
- <android>The road to android 学习之路:初次接触
- hibernate 映射视图及相应问题
- android菜鸟学习记录之---字符串资源的使用