2024-12-18 11:17:37 +08:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
2024-12-18 16:35:39 +08:00
using System.Threading.Tasks ;
2024-12-18 11:17:37 +08:00
public class CameraSmoothMove : MonoBehaviour
{
2024-12-18 16:35:39 +08:00
public Transform target ; // <20> <> <EFBFBD> <EFBFBD> Χ <EFBFBD> Ƶ<EFBFBD> Ŀ<EFBFBD> ꣨<EFBFBD> <EAA3A8> <EFBFBD> <EFBFBD> <EFBFBD>
public Vector3 offset = new Vector3 ( 0f , 2f , - 5f ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʼ ƫ<CABC> <C6AB> <EFBFBD> <EFBFBD>
private float orbitSpeed = 0.5f ; // <20> <> Ŀ<EFBFBD> <C4BF> <EFBFBD> <EFBFBD> ת<EFBFBD> <D7AA> <EFBFBD> ٶȣ <D9B6> <C8A3> <EFBFBD> /<2F> 룩
2024-12-18 19:21:11 +08:00
public float smoothSpeed = 0.5f ; // ƽ <> <C6BD> <EFBFBD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> ٶ<EFBFBD>
public float minDistance = 70f ; // <20> <> С <EFBFBD> <D0A1> <EFBFBD> <EFBFBD>
public float maxDistance = 80f ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2024-12-18 16:35:39 +08:00
public float scrollSpeed = 2f ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֿ<EFBFBD> <D6BF> ƾ<EFBFBD> <C6BE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ٶ<EFBFBD>
2024-12-18 11:17:37 +08:00
2024-12-18 16:35:39 +08:00
private bool isOrbiting = true ; // <20> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ת
2024-12-18 19:21:11 +08:00
private float orbitDuration = 10f ; // <20> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> תһ Ȧ<D2BB> <C8A6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1>
2024-12-18 16:35:39 +08:00
private Vector3 fixedPosition ; // <20> <> <EFBFBD> <EFBFBD> ͣ<EFBFBD> <CDA3> <EFBFBD> Ĺ̶<C4B9> λ<EFBFBD> <CEBB>
private Quaternion fixedRotation ; // <20> <> <EFBFBD> <EFBFBD> ͣ<EFBFBD> <CDA3> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> ת
private float currentDistance ; // <20> <> ǰ<EFBFBD> <C7B0> Ŀ<EFBFBD> <C4BF> <EFBFBD> ľ<EFBFBD> <C4BE> <EFBFBD>
private Transform mainCameraTransform ; // Main Camera <20> <> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
2024-12-18 11:17:37 +08:00
2024-12-18 19:21:11 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ӽǵ<D3BD> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
public Vector3 overlookPosition = new Vector3 ( 0f , 70f , 90f ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ӽǵij<C7B5> ʼ λ<CABC> <CEBB>
public Quaternion overlookRotation = Quaternion . Euler ( 90f , 0f , 0f ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ӽǵ<D3BD> <C7B5> <EFBFBD> ת
2024-12-23 17:28:00 +08:00
public GameObject Canvas ;
2024-12-18 16:35:39 +08:00
private void Start ( )
{
if ( target = = null )
{
Debug . LogError ( "Ŀ<> 꽨<EFBFBD> <EABDA8> <EFBFBD> <EFBFBD> δ<EFBFBD> <CEB4> <EFBFBD> ã<EFBFBD> " ) ;
return ;
}
// <20> <> ȡ Main Camera <20> <> Transform
mainCameraTransform = Camera . main . transform ;
// <20> <> <EFBFBD> ó<EFBFBD> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD>
2024-12-18 19:21:11 +08:00
currentDistance = 20f ; // <20> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EEABBE> <EFBFBD>
2024-12-18 16:35:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ת<EFBFBD> <D7AA> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> Ӧ<EFBFBD> <D3A6> ͣ<EFBFBD> <CDA3> <EFBFBD> <EFBFBD> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
fixedPosition = mainCameraTransform . position ;
fixedRotation = mainCameraTransform . rotation ;
2024-12-23 17:28:00 +08:00
Canvas . gameObject . SetActive ( false ) ;
2024-12-18 19:21:11 +08:00
// <20> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
StartCameraSequence ( ) ;
}
private void Update ( )
{
// <20> <> Ӧ<EFBFBD> <D3A6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 룬<EFBFBD> <EBA3AC> <EFBFBD> Ƹ<EFBFBD> <C6B8> <EFBFBD> <EFBFBD> ӽ<EFBFBD> <D3BD> <EFBFBD> Ŀ<EFBFBD> <C4BF> <EFBFBD> ľ<EFBFBD> <C4BE> <EFBFBD>
if ( ! isOrbiting )
2024-12-18 16:35:39 +08:00
{
2024-12-18 19:21:11 +08:00
float scrollInput = Input . GetAxis ( "Mouse ScrollWheel" ) ;
currentDistance - = scrollInput * scrollSpeed ; // <20> <> <EFBFBD> ӻ<EFBFBD> <D3BB> <EFBFBD> <EFBFBD> پ<EFBFBD> <D9BE> <EFBFBD>
currentDistance = Mathf . Clamp ( currentDistance , minDistance , maxDistance ) ;
// <20> <> <EFBFBD> ¸ <EFBFBD> <C2B8> <EFBFBD> <EFBFBD> ӽǵ<D3BD> λ<EFBFBD> <CEBB>
overlookPosition = target . position + new Vector3 ( 0f , currentDistance , 0f ) ;
2024-12-18 16:35:39 +08:00
}
}
2024-12-18 19:21:11 +08:00
// <20> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
public async void StartCameraSequence ( )
2024-12-18 16:35:39 +08:00
{
2024-12-18 19:21:11 +08:00
// 1. <20> ӵ<EFBFBD> ǰ<EFBFBD> ӽ<EFBFBD> <D3BD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӽ<EFBFBD>
await MoveToOverlookPointAsync ( ) ;
// 2. <20> Ӹ<EFBFBD> <D3B8> <EFBFBD> <EFBFBD> ӽ<EFBFBD> <D3BD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ƿ<EFBFBD> ʼ <EFBFBD> <CABC>
await MoveToOrbitStartPointAsync ( ) ;
// 3. <20> <> <EFBFBD> <EFBFBD> Ŀ<EFBFBD> <C4BF>
StartOrbit ( ) ;
2024-12-18 16:35:39 +08:00
}
// <20> <> <EFBFBD> <EFBFBD> Χ <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ת
public async void StartOrbit ( )
{
isOrbiting = true ;
await OrbitAsync ( ) ;
StopOrbit ( ) ;
}
// ֹͣΧ <D6B9> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ת<EFBFBD> <D7AA> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> ɵ<EFBFBD> Main Camera <20> <> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
public async void StopOrbit ( )
{
isOrbiting = false ;
// <20> <> ȡ Main Camera <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
fixedPosition = mainCameraTransform . position ;
fixedRotation = mainCameraTransform . rotation ;
await MoveToFixedPositionAsync ( ) ;
}
2024-12-18 11:17:37 +08:00
2024-12-18 16:35:39 +08:00
// <20> 첽<EFBFBD> <ECB2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ͨ<EFBFBD> <CDA8> async ʵ<> <CAB5> <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> תһ Ȧ
private async Task OrbitAsync ( )
2024-12-18 11:17:37 +08:00
{
2024-12-18 16:35:39 +08:00
float elapsedTime = 0f ;
while ( elapsedTime < orbitDuration )
2024-12-18 11:17:37 +08:00
{
2024-12-18 16:35:39 +08:00
if ( ! isOrbiting ) break ; // <20> <> ǰ<EFBFBD> ˳<EFBFBD>
2024-12-18 11:17:37 +08:00
elapsedTime + = Time . deltaTime ;
2024-12-18 16:35:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ת<EFBFBD> Ƕ<EFBFBD>
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
) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> λ<EFBFBD> <CEBB>
transform . position = target . position + offsetPosition ;
transform . LookAt ( target ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ŀ<EFBFBD> <C4BF>
await Task . Yield ( ) ; // <20> ȴ<EFBFBD> <C8B4> <EFBFBD> һ ֡
}
}
// <20> 첽<EFBFBD> <ECB2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> ɵ<EFBFBD> Main Camera <20> <> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
private async Task MoveToFixedPositionAsync ( )
{
float elapsedTime = 0f ;
Vector3 startPosition = transform . position ;
Quaternion startRotation = transform . rotation ;
while ( elapsedTime < 1f )
{
elapsedTime + = Time . deltaTime * smoothSpeed ;
// ƽ <> <C6BD> <EFBFBD> ز<EFBFBD> ֵ<EFBFBD> <D6B5> <EFBFBD> <EFBFBD> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת
transform . position = Vector3 . Lerp ( startPosition , fixedPosition , elapsedTime ) ;
transform . rotation = Quaternion . Lerp ( startRotation , fixedRotation , elapsedTime ) ;
2024-12-18 11:17:37 +08:00
2024-12-18 16:35:39 +08:00
await Task . Yield ( ) ; // <20> ȴ<EFBFBD> <C8B4> <EFBFBD> һ ֡
2024-12-18 11:17:37 +08:00
}
2024-12-18 16:35:39 +08:00
// ȷ<> <C8B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> λ<EFBFBD> ú<EFBFBD> <C3BA> <EFBFBD> ת<EFBFBD> <D7AA> Main Camera <20> <> <EFBFBD> <EFBFBD>
transform . position = fixedPosition ;
transform . rotation = fixedRotation ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
HideCamera ( ) ;
}
2024-12-18 19:21:11 +08:00
// <20> 첽<EFBFBD> <ECB2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ӹ<EFBFBD> <D3B8> <EFBFBD> <EFBFBD> ӽ<EFBFBD> <D3BD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ƿ<EFBFBD> ʼ <EFBFBD> <CABC>
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 ;
// ƽ <> <C6BD> <EFBFBD> ز<EFBFBD> ֵ<EFBFBD> <D6B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʼ <EFBFBD> <CABC>
transform . position = Vector3 . Lerp ( startPosition , orbitStartPosition , elapsedTime ) ;
transform . rotation = Quaternion . Lerp ( startRotation , Quaternion . LookRotation ( target . position - orbitStartPosition ) , elapsedTime ) ;
await Task . Yield ( ) ; // <20> ȴ<EFBFBD> <C8B4> <EFBFBD> һ ֡
}
}
// <20> 첽<EFBFBD> <ECB2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> ǰ<EFBFBD> ӽ<EFBFBD> <D3BD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӽ<EFBFBD>
private async Task MoveToOverlookPointAsync ( )
{
float elapsedTime = 0f ;
Vector3 startPosition = transform . position ;
Quaternion startRotation = transform . rotation ;
while ( elapsedTime < 1f ) // <20> <> <EFBFBD> <EFBFBD> ƽ <EFBFBD> <C6BD> <EFBFBD> <EFBFBD> <EFBFBD> ɵij<C9B5> <C4B3> <EFBFBD> ʱ<EFBFBD> <CAB1>
{
elapsedTime + = Time . deltaTime * smoothSpeed ;
// <20> ӵ<EFBFBD> ǰλ<C7B0> <CEBB> ƽ <EFBFBD> <C6BD> <EFBFBD> ƶ<EFBFBD> <C6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> λ<EFBFBD> <CEBB>
transform . position = Vector3 . Lerp ( startPosition , overlookPosition , elapsedTime ) ;
transform . rotation = Quaternion . Lerp ( startRotation , overlookRotation , elapsedTime ) ;
await Task . Yield ( ) ; // <20> ȴ<EFBFBD> <C8B4> <EFBFBD> һ ֡
}
// ȷ<> <C8B7> <EFBFBD> <EFBFBD> <EFBFBD> ︩<EFBFBD> <EFB8A9> <EFBFBD> ӽ<EFBFBD> λ<EFBFBD> <CEBB>
transform . position = overlookPosition ;
transform . rotation = overlookRotation ;
}
2024-12-18 16:35:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ķ<EFBFBD> <C4B7> <EFBFBD>
private void HideCamera ( )
{
2024-12-23 17:28:00 +08:00
2024-12-18 16:35:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> 1<> <31> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Camera <20> <> <EFBFBD> <EFBFBD>
GetComponent < Camera > ( ) . enabled = false ;
2024-12-23 17:28:00 +08:00
Canvas . gameObject . SetActive ( true ) ;
2024-12-18 16:35:39 +08:00
Debug . Log ( "<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " ) ;
2024-12-18 11:17:37 +08:00
}
}
2024-12-18 16:35:39 +08:00