_xiaofang/xiaofang/Assets/Script/hylScripts/CameraSmoothMove.cs

141 lines
4.1 KiB
C#
Raw Normal View History

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>
public float smoothSpeed = 0.125f; // ƽ<><C6BD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ٶ<EFBFBD>
public float minDistance = 2f; // <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
public float maxDistance = 15f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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>ת
private float orbitDuration = 10f; // <20>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>תһȦ<D2BB><C8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
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 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>
currentDistance = -offset.z;
// <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;
if (isOrbiting)
{
StartOrbit();
}
}
private void Update()
{
// <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
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>Χ<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();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
private void HideCamera()
{
// <20><><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Camera <20><><EFBFBD><EFBFBD>
GetComponent<Camera>().enabled = false;
// <20><><EFBFBD><EFBFBD> 2<><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> GameObject
// gameObject.SetActive(false);
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