fix: 优化了MainCamera脚本的性能
This commit is contained in:
parent
2290812228
commit
ce5d68721d
|
@ -9,7 +9,6 @@ public class CameraBehaviour : MonoBehaviour
|
|||
private const int MouseZoomMax = 20; //相机距离最大值
|
||||
private const float XSpeed = 250.0f;
|
||||
private const float YSpeed = 120.0f;
|
||||
|
||||
private const int YMinLimit = -360;
|
||||
private const int YMaxLimit = 360;
|
||||
|
||||
|
@ -23,11 +22,11 @@ public class CameraBehaviour : MonoBehaviour
|
|||
private Vector3 _initPosition; //平移时用于存储平移的起点位置
|
||||
private Vector3 _cameraX; //相机的x轴方向向量
|
||||
private Vector3 _cameraY; //相机的y轴方向向量
|
||||
private Vector3 _cameraZ; //相机的z轴方向向量
|
||||
|
||||
private Vector3 _initScreenPosition; //中键刚按下时鼠标的屏幕坐标(第三个值其实没什么用)
|
||||
private Vector3 _cursorScreenPosition; //当前鼠标的屏幕坐标(第三个值其实没什么用)
|
||||
|
||||
private Camera _mainCamera;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
//这里就是设置一下初始的相机视角以及一些其他变量,这里的x和y。。。是和下面getAxis的mouse x与mouse y对应
|
||||
|
@ -36,13 +35,16 @@ private void Start()
|
|||
_eulerY = angles.x;
|
||||
_targetPosition = target.position;
|
||||
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
|
||||
|
||||
// 引入中间变量 提高代码效率
|
||||
var transform1 = transform;
|
||||
transform1.rotation = _cameraRotation; //设置相机姿态
|
||||
/*var position =
|
||||
_cameraRotation * new Vector3(0.0F, 0.0F, -_distance) +
|
||||
_targetPosition; //四元数表示一个旋转,四元数乘以向量相当于把向量旋转对应角度,然后加上目标物体的位置就是相机位置了*/
|
||||
transform1.position = _cameraRotation * new Vector3(0, 0, -_distance) + _targetPosition; //设置相机位置
|
||||
//设置相机姿态
|
||||
transform1.rotation = _cameraRotation;
|
||||
//设置相机位置
|
||||
transform1.position = _cameraRotation * new Vector3(0, 0, -_distance) + _targetPosition;
|
||||
|
||||
// 获得主摄像机
|
||||
_mainCamera = Camera.main;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
@ -65,7 +67,7 @@ private void Update()
|
|||
}
|
||||
else if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能
|
||||
{
|
||||
if (_distance >= MouseZoomMin && _distance <= MouseZoomMax)
|
||||
if (_distance is >= MouseZoomMin and <= MouseZoomMax)
|
||||
{
|
||||
_distance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
|
||||
}
|
||||
|
@ -91,12 +93,11 @@ private void Update()
|
|||
var transform1 = transform;
|
||||
_cameraX = transform1.right;
|
||||
_cameraY = transform1.up;
|
||||
_cameraZ = transform1.forward;
|
||||
|
||||
_initScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _targetOnScreenPosition.z);
|
||||
|
||||
//targetOnScreenPosition.z为目标物体到相机平面的法线距离
|
||||
_targetOnScreenPosition = Camera.main!.WorldToScreenPoint(_targetPosition);
|
||||
_targetOnScreenPosition = _mainCamera.WorldToScreenPoint(_targetPosition);
|
||||
_initPosition = _targetPosition;
|
||||
}
|
||||
|
||||
|
@ -117,7 +118,6 @@ private void Update()
|
|||
|
||||
if (Input.GetMouseButtonUp(2))
|
||||
{
|
||||
Debug.Log("upOnce");
|
||||
//平移结束把cameraTargetPosition的位置更新一下,不然会影响缩放与旋转功能
|
||||
_targetPosition = target.position;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user