fix: 修复了摄像机角度位置初始化错误的问题
This commit is contained in:
parent
ce5d68721d
commit
d9acef1e18
|
@ -7,15 +7,14 @@ public class CameraBehaviour : MonoBehaviour
|
||||||
private const int MouseWheelSensitivity = 1; //滚轮灵敏度设置
|
private const int MouseWheelSensitivity = 1; //滚轮灵敏度设置
|
||||||
private const int MouseZoomMin = 1; //相机距离最小值
|
private const int MouseZoomMin = 1; //相机距离最小值
|
||||||
private const int MouseZoomMax = 20; //相机距离最大值
|
private const int MouseZoomMax = 20; //相机距离最大值
|
||||||
private const float XSpeed = 250.0f;
|
private const float XSpeed = 120.0f;
|
||||||
private const float YSpeed = 120.0f;
|
private const float YSpeed = 250.0f;
|
||||||
private const int YMinLimit = -360;
|
private const int YMinLimit = -360;
|
||||||
private const int YMaxLimit = 360;
|
private const int YMaxLimit = 360;
|
||||||
|
|
||||||
private float _eulerX; //存储相机的euler角
|
private float _eulerX; //存储相机的euler角
|
||||||
private float _eulerY; //存储相机的euler角
|
private float _eulerY; //存储相机的euler角
|
||||||
|
private float _distance; //相机和target之间的距离,因为相机的Z轴总是指向target,也就是相机z轴方向上的距离
|
||||||
private float _distance = 5; //相机和target之间的距离,因为相机的Z轴总是指向target,也就是相机z轴方向上的距离
|
|
||||||
private Vector3 _targetOnScreenPosition; //目标的屏幕坐标,第三个值为z轴距离
|
private Vector3 _targetOnScreenPosition; //目标的屏幕坐标,第三个值为z轴距离
|
||||||
private Quaternion _cameraRotation; //存储相机的姿态四元数
|
private Quaternion _cameraRotation; //存储相机的姿态四元数
|
||||||
private Vector3 _targetPosition; //target的位置
|
private Vector3 _targetPosition; //target的位置
|
||||||
|
@ -31,10 +30,12 @@ private void Start()
|
||||||
{
|
{
|
||||||
//这里就是设置一下初始的相机视角以及一些其他变量,这里的x和y。。。是和下面getAxis的mouse x与mouse y对应
|
//这里就是设置一下初始的相机视角以及一些其他变量,这里的x和y。。。是和下面getAxis的mouse x与mouse y对应
|
||||||
var angles = transform.eulerAngles;
|
var angles = transform.eulerAngles;
|
||||||
_eulerX = angles.y;
|
_eulerX = angles.x;
|
||||||
_eulerY = angles.x;
|
_eulerY = angles.y;
|
||||||
_targetPosition = target.position;
|
var position = target.position;
|
||||||
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
|
_targetPosition = position;
|
||||||
|
_cameraRotation = Quaternion.Euler(_eulerX, _eulerY, 0);
|
||||||
|
_distance = Vector3.Distance(position, transform.position);
|
||||||
|
|
||||||
// 引入中间变量 提高代码效率
|
// 引入中间变量 提高代码效率
|
||||||
var transform1 = transform;
|
var transform1 = transform;
|
||||||
|
@ -52,12 +53,12 @@ private void Update()
|
||||||
//鼠标右键旋转功能
|
//鼠标右键旋转功能
|
||||||
if (Input.GetMouseButton(1))
|
if (Input.GetMouseButton(1))
|
||||||
{
|
{
|
||||||
_eulerX += Input.GetAxis("Mouse X") * XSpeed * 0.02f;
|
// 鼠标控制需要反转X、Y轴
|
||||||
_eulerY -= Input.GetAxis("Mouse Y") * YSpeed * 0.02f;
|
_eulerX += Input.GetAxis("Mouse Y") * XSpeed * 0.02f;
|
||||||
|
_eulerX = ClampAngle(_eulerX, YMinLimit, YMaxLimit);
|
||||||
|
_eulerY -= Input.GetAxis("Mouse X") * YSpeed * 0.02f;
|
||||||
|
|
||||||
_eulerY = ClampAngle(_eulerY, YMinLimit, YMaxLimit);
|
_cameraRotation = Quaternion.Euler(_eulerX, _eulerY, 0);
|
||||||
|
|
||||||
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
|
|
||||||
var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition;
|
var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition;
|
||||||
|
|
||||||
// 引入中间变量 提高代码效率
|
// 引入中间变量 提高代码效率
|
||||||
|
@ -65,7 +66,8 @@ private void Update()
|
||||||
transform1.rotation = _cameraRotation;
|
transform1.rotation = _cameraRotation;
|
||||||
transform1.position = position;
|
transform1.position = position;
|
||||||
}
|
}
|
||||||
else if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能
|
|
||||||
|
if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能
|
||||||
{
|
{
|
||||||
if (_distance is >= MouseZoomMin and <= MouseZoomMax)
|
if (_distance is >= MouseZoomMin and <= MouseZoomMax)
|
||||||
{
|
{
|
||||||
|
|
|
@ -406,7 +406,7 @@ Transform:
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 963194225}
|
m_GameObject: {fileID: 963194225}
|
||||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||||
m_LocalPosition: {x: 0, y: 1, z: -10}
|
m_LocalPosition: {x: -0.3, y: 1.06, z: -8.26}
|
||||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_ConstrainProportionsScale: 0
|
m_ConstrainProportionsScale: 0
|
||||||
m_Children: []
|
m_Children: []
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"com.unity.collab-proxy": "1.17.7",
|
"com.unity.collab-proxy": "1.17.7",
|
||||||
"com.unity.feature.development": "1.0.1",
|
"com.unity.feature.development": "1.0.1",
|
||||||
"com.unity.ide.rider": "3.0.16",
|
"com.unity.ide.rider": "3.0.18",
|
||||||
"com.unity.ide.visualstudio": "2.0.16",
|
"com.unity.ide.visualstudio": "2.0.16",
|
||||||
"com.unity.ide.vscode": "1.2.5",
|
"com.unity.ide.vscode": "1.2.5",
|
||||||
"com.unity.test-framework": "1.1.31",
|
"com.unity.test-framework": "1.1.31",
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"com.unity.ide.rider": {
|
"com.unity.ide.rider": {
|
||||||
"version": "3.0.16",
|
"version": "3.0.18",
|
||||||
"depth": 0,
|
"depth": 0,
|
||||||
"source": "registry",
|
"source": "registry",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user