136 lines
4.4 KiB
C#
136 lines
4.4 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using System.Threading.Tasks;
|
|||
|
public class Godvisual : MonoBehaviour
|
|||
|
{
|
|||
|
public Transform mainBuilding; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
|
|||
|
public Transform birdEyeView; // <20><><EFBFBD><EFBFBD><EFBFBD>ӽǵ<D3BD> Transform
|
|||
|
public Transform[] characterViews; // ÿ<><C3BF><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>Ĭ<EFBFBD><C4AC><EFBFBD>ӽ<EFBFBD> Transform <20><><EFBFBD><EFBFBD>
|
|||
|
public bool ishost; // <20>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public float voiceLength = 10f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
public float rotationRadius = 100f; // <20><><EFBFBD>ư뾶
|
|||
|
public float rotationHeight = 5f; // <20><><EFBFBD>Ƹ߶<C6B8>
|
|||
|
public float rotationSpeed = 36f; // <20><><EFBFBD><EFBFBD><EFBFBD>ٶȣ<D9B6><C8A3><EFBFBD><EFBFBD>ٶȣ<D9B6><C8A3><EFBFBD>/<2F>룩
|
|||
|
private float elapsedTime = 0f; // <20>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
private bool isSwitching = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB>ӽ<EFBFBD>
|
|||
|
private bool isRotating = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
|||
|
private bool isAtGodView = true; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>ӽ<EFBFBD>
|
|||
|
void OnEnable()
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
Starthost.OnBoolValueChanged += HandleRotationControl;
|
|||
|
}
|
|||
|
|
|||
|
void OnDisable()
|
|||
|
{
|
|||
|
// ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
Starthost.OnBoolValueChanged -= HandleRotationControl;
|
|||
|
}
|
|||
|
|
|||
|
// Start is called before the first frame update
|
|||
|
void Start()
|
|||
|
{
|
|||
|
// <20>ʼ<EEBFAA>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>ӽ<EFBFBD>
|
|||
|
SetToGodView();
|
|||
|
|
|||
|
elapsedTime = 0f;
|
|||
|
isSwitching = false;
|
|||
|
|
|||
|
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC><CEBB><EFBFBD>ڻ<EFBFBD><DABB>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//Vector3 startPosition = mainBuilding.position + new Vector3(rotationRadius, rotationHeight, 0);
|
|||
|
//transform.position = startPosition;
|
|||
|
//transform.LookAt(mainBuilding); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>ճ<EFBFBD><D5B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>ӽ<EFBFBD>
|
|||
|
void SetToGodView()
|
|||
|
{
|
|||
|
transform.position = birdEyeView.position;
|
|||
|
transform.rotation = birdEyeView.rotation;
|
|||
|
isAtGodView = true;
|
|||
|
isRotating = false; // ֹͣ<CDA3><D6B9>ת<EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
// Update is called once per frame
|
|||
|
void Update()
|
|||
|
{
|
|||
|
if (!isRotating) return;
|
|||
|
// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
elapsedTime += Time.deltaTime;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (elapsedTime < voiceLength - 2f && !isSwitching)
|
|||
|
{
|
|||
|
RotateAroundBuilding();
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ӽ<EFBFBD>
|
|||
|
else if (!isSwitching)
|
|||
|
{
|
|||
|
isSwitching = true;
|
|||
|
SwitchToFinalViewAsync();
|
|||
|
}
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void RotateAroundBuilding()
|
|||
|
{
|
|||
|
float angle = rotationSpeed * Time.deltaTime; // ÿ֡<C3BF><D6A1>ת<EFBFBD>ĽǶ<C4BD>
|
|||
|
Vector3 offset = transform.position - mainBuilding.position; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ƫ<EFBFBD><C6AB>
|
|||
|
offset = Quaternion.Euler(0, angle, 0) * offset; // <20>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>ת
|
|||
|
transform.position = mainBuilding.position + offset; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
transform.position = new Vector3(transform.position.x, mainBuilding.position.y + rotationHeight, transform.position.z); // ȷ<><C8B7><EFBFBD>߶ȹ̶<C8B9>
|
|||
|
transform.LookAt(mainBuilding); // ʼ<>ճ<EFBFBD><D5B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽǣ<D3BD>ʹ<EFBFBD><CAB9> async/await<69><74>
|
|||
|
async void SwitchToFinalViewAsync()
|
|||
|
{
|
|||
|
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ɫ<EFBFBD>ӽ<EFBFBD>
|
|||
|
Transform targetView = ishost ? birdEyeView : GetCurrentCharacterView();
|
|||
|
|
|||
|
// ƽ<><C6BD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>ת
|
|||
|
float switchDuration = 2f; // <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
Vector3 startPosition = transform.position;
|
|||
|
Quaternion startRotation = transform.rotation;
|
|||
|
Vector3 endPosition = targetView.position;
|
|||
|
Quaternion endRotation = targetView.rotation;
|
|||
|
|
|||
|
// ƽ<><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (float t = 0; t < switchDuration; t += Time.deltaTime)
|
|||
|
{
|
|||
|
float progress = t / switchDuration;
|
|||
|
transform.position = Vector3.Lerp(startPosition, endPosition, progress);
|
|||
|
transform.rotation = Quaternion.Lerp(startRotation, endRotation, progress);
|
|||
|
await Task.Yield(); // <20>ȴ<EFBFBD>һ֡
|
|||
|
}
|
|||
|
|
|||
|
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>תȷ
|
|||
|
transform.position = endPosition;
|
|||
|
transform.rotation = endRotation;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>ɫ<EFBFBD><C9AB>Ĭ<EFBFBD><C4AC><EFBFBD>ӽǣ<D3BD><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ϊ 0<><30>
|
|||
|
Transform GetCurrentCharacterView()
|
|||
|
{
|
|||
|
int characterIndex = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DFBC><EFBFBD>̬<EFBFBD><CCAC>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
|
|||
|
if (characterIndex >= 0 && characterIndex < characterViews.Length)
|
|||
|
{
|
|||
|
return characterViews[characterIndex];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Debug.LogWarning("<22><>ɫ<EFBFBD>ӽ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ã<EFBFBD>ʹ<EFBFBD><CAB9>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD>");
|
|||
|
return birdEyeView;
|
|||
|
}
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
void HandleRotationControl(bool shouldRotate)
|
|||
|
{
|
|||
|
isRotating = shouldRotate;
|
|||
|
if (!shouldRotate)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>״̬
|
|||
|
elapsedTime = 0f;
|
|||
|
isSwitching = false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|