NG_/Assets/预制体/渲染后整体打包/AutoMoveCamera.cs

117 lines
4.7 KiB
C#
Raw Normal View History

2024-12-13 19:40:05 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoMoveCamera : MonoBehaviour
{
public Camera camera;
public GameObject parentTarget;
public List<GameObject> showTargets; // 目标物体
// 接近目标
//public List<GameObject> targets; // 目标物体
public static int schedule=0;
public static int runState = 1;
// Start is called before the first frame update
void Start()
{
2024-12-20 19:44:46 +08:00
StartCoroutine(autoMove());
2024-12-13 19:40:05 +08:00
}
private IEnumerator autoMove()
{
while (true)
{
2024-12-20 19:44:46 +08:00
if (runState == 1)
2024-12-13 19:40:05 +08:00
{
2024-12-20 19:44:46 +08:00
// 平滑移动
// 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)
2024-12-13 19:40:05 +08:00
{
2024-12-20 19:44:46 +08:00
camera.transform.position = parentTarget.transform.GetChild(schedule).position;
camera.transform.rotation = parentTarget.transform.GetChild(schedule).rotation;
schedule++;
yield return new WaitForSeconds(2f);
if (schedule==parentTarget.transform.childCount)
{
schedule = 0;
runState = 2;
foreach (GameObject showTarget in showTargets)
{
showTarget.SetActive(false);
}
}
2024-12-13 19:40:05 +08:00
}
}
2024-12-20 19:44:46 +08:00
yield return null;
2024-12-13 19:40:05 +08:00
}
}
// Update is called once per frame
void Update()
{
2024-12-20 19:44:46 +08:00
/*if (runState == 1)
2024-12-13 19:40:05 +08:00
{
// 平滑移动
// 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);
}
}
}
2024-12-20 19:44:46 +08:00
}*/
2024-12-13 19:40:05 +08:00
}
}