NG_/Scripts/3D/摄像机/AutoMoveCamera2.cs

71 lines
2.4 KiB
C#
Raw Normal View History

2024-12-13 18:49:44 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoMoveCamera2 : MonoBehaviour
{
public Camera camera;
public GameObject parentTarget;
public List<GameObject> showTargets; // 目标物体
// 接近目标
//public List<GameObject> targets; // 目标物体
public static int schedule=0;
// Start is called before the first frame update
void Start()
{
StartCoroutine(autoMove());
}
private IEnumerator autoMove()
{
while (true)
{
if (AutoMoveCamera.runState==2)
{
// 目标位置
Vector3 targetPosition = parentTarget.transform.GetChild(schedule).position;
// 计算每帧的匀速移动
float speed = 5f; // 设定你希望摄像机的匀速速度(单位:每秒的移动距离)
camera.transform.position = Vector3.MoveTowards(camera.transform.position, targetPosition, speed * Time.deltaTime);
float rotationSpeed = 30f; // 旋转速度
camera.transform.rotation = Quaternion.RotateTowards(camera.transform.rotation, parentTarget.transform.GetChild(schedule).rotation, rotationSpeed * Time.deltaTime);
// 检查是否接近目标位置和旋转
if (Vector3.Distance(camera.transform.position, parentTarget.transform.GetChild(schedule).position) < 0.1f &&
Quaternion.Angle(camera.transform.rotation, parentTarget.transform.GetChild(schedule).rotation) < 0.1f)
{
camera.transform.position = parentTarget.transform.GetChild(schedule).position;
camera.transform.rotation = parentTarget.transform.GetChild(schedule).rotation;
schedule++;
yield return new WaitForSeconds(6f);
if (schedule==parentTarget.transform.childCount)
{
schedule = 0;
AutoMoveCamera.runState = 1;
foreach (GameObject showTarget in showTargets)
{
showTarget.SetActive(true);
}
}
}
}
yield return null;
}
}
// Update is called once per frame
void Update()
{
}
}