Files
P2SinNext/Assets/Code/CameraController.cs
2025-10-19 19:04:50 +05:00

132 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[Header("Camera Settings")]
public float cameraHeight = 600f; // Высота камеры над целью
public float cameraDistance = 700f; // Расстояние от камеры до цели
public float rotationSpeed = 5f; // Скорость плавного поворота
public Vector3 cameraTargetPoint; // Точка, на которую смотрит камера
public uint sourceX;
public uint sourceY;
public uint sourceZ;
public int currentCameraRotation = 0; // Текущий угол вращения (0-7)
private bool isCameraRotating = false;
private Quaternion targetCameraRotation;
public void SetCamTargetXY(uint rotate, uint xx, uint yy, uint zz)
{
sourceX = xx;
sourceY = yy;
sourceZ = zz;
float targetX = (int)xx;
float targetY = (int)yy;
float targetZ = (int)zz;
// Устанавливаем целевую точку
cameraTargetPoint = new Vector3(targetX, targetY, targetZ);
// Преобразуем вращение (0-7 в 0-315 градусов с шагом 45)
float targetAngle = ResourceManager.ConvertGameRotation(rotate);
// Обновляем текущее вращение
currentCameraRotation = (int)rotate;
// Немедленно устанавливаем позицию и вращение камеры
UpdateCameraPositionImmediate(targetAngle);
//Debug.Log($"Camera target: {cameraTargetPoint}, Rotation: {targetAngle}° (index: {rotate})");
}
// Немедленное обновление позиции камеры
private void UpdateCameraPositionImmediate(float angle)
{
// Преобразуем угол в радианы
float radAngle = angle * Mathf.Deg2Rad;
// Вычисляем смещение камеры от целевой точки
Vector3 cameraOffset = new Vector3(
Mathf.Sin(radAngle) * cameraDistance,
cameraHeight, // Фиксированная высота над целью
Mathf.Cos(radAngle) * cameraDistance
);
// Устанавливаем позицию камеры
transform.position = cameraTargetPoint + cameraOffset;
// Камера всегда смотрит на целевую точку
transform.LookAt(cameraTargetPoint);
}
// Метод для плавного поворота камеры на заданный угол (если понадобится)
public void RotateCameraSmoothly(int targetRotationIndex)
{
if (isCameraRotating) return;
targetRotationIndex = targetRotationIndex % 8;
if (targetRotationIndex == currentCameraRotation) return;
StartCoroutine(SmoothRotationCoroutine(targetRotationIndex));
}
// Корутина для плавного вращения
private IEnumerator SmoothRotationCoroutine(int targetRotationIndex)
{
isCameraRotating = true;
float startAngle = currentCameraRotation * 45f;
float targetAngle = targetRotationIndex * 45f;
float progress = 0f;
// Корректируем для кратчайшего пути вращения
float angleDifference = Mathf.DeltaAngle(startAngle, targetAngle);
if (Mathf.Abs(angleDifference) > 180f)
{
targetAngle = startAngle + (angleDifference > 0 ? angleDifference - 360f : angleDifference + 360f);
}
Vector3 startPosition = transform.position;
while (progress < 1f)
{
progress += Time.deltaTime * rotationSpeed;
float currentAngle = Mathf.Lerp(startAngle, targetAngle, progress);
// Обновляем позицию камеры
float radAngle = currentAngle * Mathf.Deg2Rad;
Vector3 cameraOffset = new Vector3(
Mathf.Sin(radAngle) * cameraDistance,
cameraHeight,
Mathf.Cos(radAngle) * cameraDistance
);
transform.position = cameraTargetPoint + cameraOffset;
transform.LookAt(cameraTargetPoint);
yield return null;
}
// Финализируем позицию
currentCameraRotation = targetRotationIndex;
UpdateCameraPositionImmediate(targetRotationIndex * 45f);
isCameraRotating = false;
}
// Update is called once per frame
void Update()
{
}
}