相机移动的调整

This commit is contained in:
huyulong 2024-12-18 19:21:11 +08:00
parent 2b9e436ea1
commit a667d525be
2 changed files with 125 additions and 63 deletions

View File

@ -25502,6 +25502,52 @@ Transform:
m_CorrespondingSourceObject: {fileID: 426904, guid: 41995a67da46cd041b30345cc6dff096, type: 3}
m_PrefabInstance: {fileID: 1593349413}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1594550430
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1594550432}
- component: {fileID: 1594550431}
m_Layer: 0
m_Name: BurstCollisionWorld
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1594550431
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1594550430}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3}
m_Name:
m_EditorClassIdentifier:
cellSpans:
m_AlignBytes: 16
--- !u!4 &1594550432
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1594550430}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 38
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &1595504383
MeshCollider:
m_ObjectHideFlags: 0
@ -26686,11 +26732,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
target: {fileID: 88965377508065921}
offset: {x: 0, y: 8, z: -1}
orbitSpeed: 2
smoothSpeed: 0.125
minDistance: 80
maxDistance: 80
scrollSpeed: 2
overlookPosition: {x: 0, y: 50, z: 0}
overlookRotation: {x: -0.45922917, y: 0.5376882, z: -0.5376882, w: -0.45922917}
--- !u!1001 &1709243970
PrefabInstance:
m_ObjectHideFlags: 0
@ -27996,52 +28043,6 @@ MeshCollider:
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: -7353403159277181372, guid: 00003e04dc9a2534eb207f108c7b0cd3, type: 3}
--- !u!1 &1821068508
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1821068510}
- component: {fileID: 1821068509}
m_Layer: 0
m_Name: BurstCollisionWorld
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1821068509
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1821068508}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3}
m_Name:
m_EditorClassIdentifier:
cellSpans:
m_AlignBytes: 16
--- !u!4 &1821068510
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1821068508}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 38
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1822675538
PrefabInstance:
m_ObjectHideFlags: 0

View File

@ -8,18 +8,22 @@ public class CameraSmoothMove : MonoBehaviour
public Transform target; // 相机围绕的目标(建筑物)
public Vector3 offset = new Vector3(0f, 2f, -5f); // 相机初始偏移量
private float orbitSpeed = 0.5f; // 绕目标旋转的速度(度/秒)
public float smoothSpeed = 0.125f; // 平滑移动的速度
public float minDistance = 2f; // 最小距离
public float maxDistance = 15f; // 最大距离
public float smoothSpeed = 0.5f; // 平滑移动的速度
public float minDistance = 70f; // 最小距离
public float maxDistance = 80f; // 最大距离
public float scrollSpeed = 2f; // 鼠标滚轮控制距离的速度
private bool isOrbiting = true; // 是否正在绕建筑物旋转
private float orbitDuration = 10f; // 绕建筑物转一圈所需的时间
private float orbitDuration = 10f; // 绕建筑物转一圈所需的时间
private Vector3 fixedPosition; // 相机停留的固定位置
private Quaternion fixedRotation; // 相机停留时的旋转
private float currentDistance; // 当前与目标的距离
private Transform mainCameraTransform; // Main Camera 的位置和旋转
// 俯瞰视角的位置和旋转
public Vector3 overlookPosition = new Vector3(0f, 70f, 90f); // 俯瞰视角的初始位置
public Quaternion overlookRotation = Quaternion.Euler(90f, 0f, 0f); // 俯瞰视角的旋转
private void Start()
{
if (target == null)
@ -32,24 +36,41 @@ public class CameraSmoothMove : MonoBehaviour
mainCameraTransform = Camera.main.transform;
// 设置初始距离
currentDistance = -offset.z;
currentDistance = 20f; // 初始俯瞰距离
// 计算旋转结束时相机应该停留的位置和旋转
fixedPosition = mainCameraTransform.position;
fixedRotation = mainCameraTransform.rotation;
if (isOrbiting)
{
StartOrbit();
}
// 开始整个流程
StartCameraSequence();
}
private void Update()
{
// 响应鼠标滚轮输入,控制与目标的距离
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
currentDistance -= scrollInput * scrollSpeed; // 增加或减少距离
currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
// 响应鼠标滚轮输入,控制俯瞰视角与目标的距离
if (!isOrbiting)
{
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
currentDistance -= scrollInput * scrollSpeed; // 增加或减少距离
currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
// 更新俯瞰视角的位置
overlookPosition = target.position + new Vector3(0f, currentDistance, 0f);
}
}
// 开始相机控制流程
public async void StartCameraSequence()
{
// 1. 从当前视角移动到俯瞰视角
await MoveToOverlookPointAsync();
// 2. 从俯瞰视角移动到环绕开始点
await MoveToOrbitStartPointAsync();
// 3. 环绕目标
StartOrbit();
}
// 启动围绕建筑物旋转
@ -125,15 +146,55 @@ public class CameraSmoothMove : MonoBehaviour
HideCamera();
}
// 异步方法:从俯瞰视角移动到环绕开始点
private async Task MoveToOrbitStartPointAsync()
{
float elapsedTime = 0f;
Vector3 startPosition = transform.position;
Quaternion startRotation = transform.rotation;
Vector3 orbitStartPosition = target.position + offset;
while (elapsedTime < 1f)
{
elapsedTime += Time.deltaTime * smoothSpeed;
// 平滑地插值到环绕起始点
transform.position = Vector3.Lerp(startPosition, orbitStartPosition, elapsedTime);
transform.rotation = Quaternion.Lerp(startRotation, Quaternion.LookRotation(target.position - orbitStartPosition), elapsedTime);
await Task.Yield(); // 等待下一帧
}
}
// 异步方法:从当前视角移动到俯瞰视角
private async Task MoveToOverlookPointAsync()
{
float elapsedTime = 0f;
Vector3 startPosition = transform.position;
Quaternion startRotation = transform.rotation;
while (elapsedTime < 1f) // 调整平滑过渡的持续时间
{
elapsedTime += Time.deltaTime * smoothSpeed;
// 从当前位置平滑移动到俯瞰位置
transform.position = Vector3.Lerp(startPosition, overlookPosition, elapsedTime);
transform.rotation = Quaternion.Lerp(startRotation, overlookRotation, elapsedTime);
await Task.Yield(); // 等待下一帧
}
// 确保到达俯瞰视角位置
transform.position = overlookPosition;
transform.rotation = overlookRotation;
}
// 隐藏相机的方法
private void HideCamera()
{
// 方法 1禁用 Camera 组件
GetComponent<Camera>().enabled = false;
// 方法 2禁用整个 GameObject
// gameObject.SetActive(false);
Debug.Log("相机已隐藏");
}
}