using System.Collections; using System.Collections.Generic; using UnityEngine; public class AutoMoveCamera : MonoBehaviour { public Camera camera; public GameObject parentTarget; public List showTargets; // 目标物体 // 接近目标 //public List targets; // 目标物体 public static int schedule=0; public static int runState = 1; // Start is called before the first frame update void Start() { // StartCoroutine(autoMove()); } private IEnumerator autoMove() { while (true) { // 平滑移动 camera.transform.position = Vector3.Lerp(camera.transform.position, parentTarget.transform.GetChild(schedule).position, 0.05f ); // 平滑旋转 camera.transform.rotation = Quaternion.Slerp(camera.transform.rotation, parentTarget.transform.GetChild(schedule).rotation, 0.1f); // 检查是否接近目标位置和旋转 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++; if (schedule==parentTarget.transform.childCount) { schedule = 0; } yield return new WaitForSeconds(2f); // 等待两秒 } } // yield return new WaitForSeconds(2f); // 等待两秒 } // Update is called once per frame void Update() { if (runState == 1) { // 平滑移动 // camera.transform.position = Vector3.Lerp(camera.transform.position, parentTarget.transform.GetChild(schedule).position, 0.01f ); // 目标位置 Vector3 targetPosition = parentTarget.transform.GetChild(schedule).position; // 计算每帧的匀速移动 float speed = 5f; // 设定你希望摄像机的匀速速度(单位:每秒的移动距离) camera.transform.position = Vector3.MoveTowards(camera.transform.position, targetPosition, speed * Time.deltaTime); // 平滑旋转 // camera.transform.rotation = Quaternion.Slerp(camera.transform.rotation, parentTarget.transform.GetChild(schedule).rotation, 0.01f); 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++; if (schedule==parentTarget.transform.childCount) { schedule = 0; runState = 2; foreach (GameObject showTarget in showTargets) { showTarget.SetActive(false); } } } } } }