This commit is contained in:
杨号敬 2024-12-18 17:11:48 +08:00
commit 338bdf8205
2 changed files with 312 additions and 97 deletions

View File

@ -4122,7 +4122,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 0c9b084f2070bda45b87da961d2eb0f6, type: 3}
propertyPath: m_RootOrder
value: 9
value: 10
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0c9b084f2070bda45b87da961d2eb0f6, type: 3}
propertyPath: m_LocalPosition.x
@ -4880,7 +4880,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 22
m_RootOrder: 23
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &159663861
PrefabInstance:
@ -6192,7 +6192,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 15
m_RootOrder: 16
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &339719735
MeshCollider:
@ -6692,7 +6692,7 @@ Transform:
- {fileID: 272025130}
- {fileID: 1600741273}
m_Father: {fileID: 0}
m_RootOrder: 31
m_RootOrder: 32
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!65 &382802813
BoxCollider:
@ -7177,7 +7177,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 25528d3d2120cee47bccf93decd4fbdf, type: 3}
propertyPath: m_RootOrder
value: 3
value: 4
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 25528d3d2120cee47bccf93decd4fbdf, type: 3}
propertyPath: m_LocalPosition.x
@ -8081,7 +8081,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 28
m_RootOrder: 29
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &479940076
MonoBehaviour:
@ -8211,7 +8211,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 454dc3d5cb346c148b640203a8bc1174, type: 3}
propertyPath: m_RootOrder
value: 2
value: 3
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 454dc3d5cb346c148b640203a8bc1174, type: 3}
propertyPath: m_LocalScale.x
@ -8377,7 +8377,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 17
m_RootOrder: 18
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &542563896
GameObject:
@ -8872,7 +8872,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 21
m_RootOrder: 22
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &583497863
PrefabInstance:
@ -9615,7 +9615,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 25
m_RootOrder: 26
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &673038100
PrefabInstance:
@ -9838,7 +9838,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 33
m_RootOrder: 34
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &702577176
MonoBehaviour:
@ -10061,7 +10061,7 @@ Transform:
- {fileID: 1973166535}
- {fileID: 1629676686}
m_Father: {fileID: 0}
m_RootOrder: 12
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &713484458
MeshCollider:
@ -10329,7 +10329,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 7104765112034956015, guid: 6815aad4cd6cb734a9d63a7210677333, type: 3}
propertyPath: m_RootOrder
value: 16
value: 17
objectReference: {fileID: 0}
- target: {fileID: 7104765112034956015, guid: 6815aad4cd6cb734a9d63a7210677333, type: 3}
propertyPath: m_LocalPosition.x
@ -10963,7 +10963,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 27
m_RootOrder: 28
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &810501282
MonoBehaviour:
@ -11329,7 +11329,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 13
m_RootOrder: 14
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &858128596 stripped
GameObject:
@ -12435,7 +12435,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 11
m_RootOrder: 12
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &984914341
PrefabInstance:
@ -12543,7 +12543,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 19
m_RootOrder: 20
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &996888830
MonoBehaviour:
@ -12929,7 +12929,7 @@ RectTransform:
- {fileID: 1081784991}
- {fileID: 1359335859}
m_Father: {fileID: 0}
m_RootOrder: 14
m_RootOrder: 15
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -14223,52 +14223,6 @@ Transform:
m_CorrespondingSourceObject: {fileID: 454008, guid: c9aa675884b75594b9a1c43c5c4e5c7a, type: 3}
m_PrefabInstance: {fileID: 1108412769}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1108700887
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1108700889}
- component: {fileID: 1108700888}
m_Layer: 0
m_Name: BurstCollisionWorld
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1108700888
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1108700887}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3}
m_Name:
m_EditorClassIdentifier:
cellSpans:
m_AlignBytes: 16
--- !u!4 &1108700889
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1108700887}
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: 36
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1111438060
PrefabInstance:
m_ObjectHideFlags: 0
@ -20741,7 +20695,7 @@ Transform:
m_ConstrainProportionsScale: 1
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 32
m_RootOrder: 33
m_LocalEulerAnglesHint: {x: -90, y: 0, z: 0}
--- !u!64 &1263546210
MeshCollider:
@ -21061,7 +21015,7 @@ Transform:
- {fileID: 496425325}
- {fileID: 1670656697}
m_Father: {fileID: 0}
m_RootOrder: 34
m_RootOrder: 35
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &1300490224
MeshCollider:
@ -22749,7 +22703,7 @@ Transform:
- {fileID: 1882199187}
- {fileID: 119116319}
m_Father: {fileID: 0}
m_RootOrder: 29
m_RootOrder: 30
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &1453694928
MeshCollider:
@ -23994,7 +23948,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 26
m_RootOrder: 27
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1480398099
PrefabInstance:
@ -24095,7 +24049,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 24
m_RootOrder: 25
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1481704192
PrefabInstance:
@ -24753,7 +24707,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1001 &1517804048
PrefabInstance:
@ -24863,7 +24817,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 30
m_RootOrder: 31
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1526740603
MonoBehaviour:
@ -24902,8 +24856,8 @@ MonoBehaviour:
LanguageJsonFile: {fileID: 4900000, guid: 62a41129c4663ee41a06274114d28feb, type: 3}
SelectJsonFile: {fileID: 4900000, guid: 3efd15f4195557144a45659d2baa1ba3, type: 3}
TaskJsonFile: {fileID: 4900000, guid: 4dec25769ed4ed3469693b1c881248af, type: 3}
GlobalJsonFile: {fileID: 0}
SelectsJsonFile: {fileID: 0}
GlobalJsonFile: {fileID: 4900000, guid: 27ecc4e37cb448e45a2611c086cdfb90, type: 3}
SelectsJsonFile: {fileID: 4900000, guid: 3efd15f4195557144a45659d2baa1ba3, type: 3}
--- !u!1001 &1542437476
PrefabInstance:
m_ObjectHideFlags: 0
@ -25051,7 +25005,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 66a5dd237d335854380f51c11f41d9a8, type: 3}
propertyPath: m_RootOrder
value: 1
value: 2
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 66a5dd237d335854380f51c11f41d9a8, type: 3}
propertyPath: m_LocalPosition.x
@ -26633,6 +26587,110 @@ Transform:
m_CorrespondingSourceObject: {fileID: 443876, guid: be8a9b4fd6545dd4e901964c42af5f6c, type: 3}
m_PrefabInstance: {fileID: 1700457050}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1707678916
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1707678919}
- component: {fileID: 1707678920}
- component: {fileID: 1707678918}
- component: {fileID: 1707678917}
m_Layer: 0
m_Name: Camera
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &1707678917
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1707678916}
m_Enabled: 1
--- !u!20 &1707678918
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1707678916}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 0
orthographic size: 5
m_Depth: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!4 &1707678919
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1707678916}
m_LocalRotation: {x: -0, y: 0.98063743, z: -0, w: 0.1958323}
m_LocalPosition: {x: -2.15, y: 11.89, z: 40.53}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 157.413, z: 0}
--- !u!114 &1707678920
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1707678916}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 276124c99c246e840b4970f38c67029e, type: 3}
m_Name:
m_EditorClassIdentifier:
target: {fileID: 88965377508065921}
offset: {x: 0, y: 8, z: -1}
orbitSpeed: 2
smoothSpeed: 0.125
minDistance: 80
maxDistance: 80
scrollSpeed: 2
--- !u!1001 &1709243970
PrefabInstance:
m_ObjectHideFlags: 0
@ -27938,6 +27996,52 @@ 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
@ -28382,7 +28486,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 53b3ada11775bfd4282e07e6e63c968f, type: 3}
propertyPath: m_RootOrder
value: 5
value: 6
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 53b3ada11775bfd4282e07e6e63c968f, type: 3}
propertyPath: m_LocalPosition.x
@ -28919,7 +29023,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 23
m_RootOrder: 24
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1912789160
PrefabInstance:
@ -29674,7 +29778,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 69a55f19217e42746bdaf0c10a7cbc80, type: 3}
propertyPath: m_RootOrder
value: 4
value: 5
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 69a55f19217e42746bdaf0c10a7cbc80, type: 3}
propertyPath: m_LocalPosition.x
@ -30290,7 +30394,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 20
m_RootOrder: 21
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &2069562217
MeshCollider:
@ -31324,7 +31428,7 @@ Transform:
- {fileID: 2112013849}
- {fileID: 125672022}
m_Father: {fileID: 0}
m_RootOrder: 8
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &119908498058829656
MeshFilter:
@ -31849,7 +31953,7 @@ Transform:
- {fileID: 6100007915572801067}
- {fileID: 2865482816726376136}
m_Father: {fileID: 0}
m_RootOrder: 10
m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &555576767404427943
MeshRenderer:
@ -34090,7 +34194,7 @@ Transform:
m_Children:
- {fileID: 2057924365034160257}
m_Father: {fileID: 0}
m_RootOrder: 35
m_RootOrder: 36
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2057924365745864529
MonoBehaviour:
@ -35399,7 +35503,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 2907630901887348954, guid: cb61a92f105ea18418e946bf54862340, type: 3}
propertyPath: m_RootOrder
value: 18
value: 19
objectReference: {fileID: 0}
- target: {fileID: 2907630901887348954, guid: cb61a92f105ea18418e946bf54862340, type: 3}
propertyPath: m_LocalPosition.x
@ -40093,7 +40197,7 @@ PrefabInstance:
objectReference: {fileID: 1493242969}
- target: {fileID: 8479287258615548071, guid: 4bc999dc543a61148a0ba3bb1dd45a4d, type: 3}
propertyPath: m_RootOrder
value: 6
value: 7
objectReference: {fileID: 0}
- target: {fileID: 8479287258615548071, guid: 4bc999dc543a61148a0ba3bb1dd45a4d, type: 3}
propertyPath: m_LocalPosition.x

View File

@ -1,29 +1,140 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
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 scrollSpeed = 2f; // 鼠标滚轮控制距离的速度
public Transform startPoint; // 起始点
public Transform endPoint; // 终点
public float duration = 5f; // 移动所需的时间(秒)
private bool isOrbiting = true; // 是否正在绕建筑物旋转
private float orbitDuration = 10f; // 绕建筑物转一圈所需的时间
private Vector3 fixedPosition; // 相机停留的固定位置
private Quaternion fixedRotation; // 相机停留时的旋转
private float currentDistance; // 当前与目标的距离
private Transform mainCameraTransform; // Main Camera 的位置和旋转
private float elapsedTime = 0f; // 记录已经过去的时间
void Update()
private void Start()
{
// 确保起始点和终点存在
if (startPoint != null && endPoint != null)
if (target == null)
{
// 更新经过的时间
elapsedTime += Time.deltaTime;
Debug.LogError("目标建筑物未设置!");
return;
}
// 计算摄像机的位置:根据时间比例计算当前位置
float t = Mathf.Clamp01(elapsedTime / duration);
// 获取 Main Camera 的 Transform
mainCameraTransform = Camera.main.transform;
// 使用 Lerp 平滑过渡摄像机的位置
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, t);
// 设置初始距离
currentDistance = -offset.z;
// 计算旋转结束时相机应该停留的位置和旋转
fixedPosition = mainCameraTransform.position;
fixedRotation = mainCameraTransform.rotation;
if (isOrbiting)
{
StartOrbit();
}
}
private void Update()
{
// 响应鼠标滚轮输入,控制与目标的距离
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
currentDistance -= scrollInput * scrollSpeed; // 增加或减少距离
currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
}
// 启动围绕建筑物旋转
public async void StartOrbit()
{
isOrbiting = true;
await OrbitAsync();
StopOrbit();
}
// 停止围绕建筑物旋转,平滑过渡到 Main Camera 的位置和旋转
public async void StopOrbit()
{
isOrbiting = false;
// 获取 Main Camera 的最新位置和旋转
fixedPosition = mainCameraTransform.position;
fixedRotation = mainCameraTransform.rotation;
await MoveToFixedPositionAsync();
}
// 异步方法:通过 async 实现绕建筑物平滑旋转一圈
private async Task OrbitAsync()
{
float elapsedTime = 0f;
while (elapsedTime < orbitDuration)
{
if (!isOrbiting) break; // 提前退出
elapsedTime += Time.deltaTime;
// 计算旋转角度
float angle = Mathf.Lerp(0, 360f, elapsedTime / orbitDuration);
Vector3 offsetPosition = new Vector3(
Mathf.Cos(Mathf.Deg2Rad * angle) * currentDistance,
offset.y,
Mathf.Sin(Mathf.Deg2Rad * angle) * currentDistance
);
// 更新相机位置
transform.position = target.position + offsetPosition;
transform.LookAt(target); // 保持相机面向目标
await Task.Yield(); // 等待下一帧
}
}
// 异步方法:平滑过渡到 Main Camera 的位置和旋转
private async Task MoveToFixedPositionAsync()
{
float elapsedTime = 0f;
Vector3 startPosition = transform.position;
Quaternion startRotation = transform.rotation;
while (elapsedTime < 1f)
{
elapsedTime += Time.deltaTime * smoothSpeed;
// 平滑地插值相机位置和旋转
transform.position = Vector3.Lerp(startPosition, fixedPosition, elapsedTime);
transform.rotation = Quaternion.Lerp(startRotation, fixedRotation, elapsedTime);
await Task.Yield(); // 等待下一帧
}
// 确保最终位置和旋转与 Main Camera 对齐
transform.position = fixedPosition;
transform.rotation = fixedRotation;
// 隐藏相机
HideCamera();
}
// 隐藏相机的方法
private void HideCamera()
{
// 方法 1禁用 Camera 组件
GetComponent<Camera>().enabled = false;
// 方法 2禁用整个 GameObject
// gameObject.SetActive(false);
Debug.Log("相机已隐藏");
}
}