122 lines
5.0 KiB
C#
122 lines
5.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AutoMoveCamera : MonoBehaviour
|
|
{
|
|
public Camera camera;
|
|
public GameObject parentTarget;
|
|
|
|
public List<GameObject> StartShowTargets; // 开始运动时显示的物体
|
|
public List<GameObject> EndHiddenTargets; // 结束运动时隐藏的物体
|
|
private bool isStart = true;
|
|
|
|
// 接近目标
|
|
//public List<GameObject> targets; // 目标物体
|
|
|
|
public static int schedule=0;
|
|
|
|
public static int runState = 0;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
StartCoroutine(autoMove());
|
|
}
|
|
|
|
private IEnumerator autoMove()
|
|
{
|
|
while (true)
|
|
{
|
|
if (runState == 1)
|
|
{
|
|
if (isStart)
|
|
{
|
|
StartShowTargets?.ForEach(target => target.SetActive(true));
|
|
isStart = false;
|
|
}
|
|
|
|
// 平滑移动
|
|
// 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++;
|
|
yield return new WaitForSeconds(2f);
|
|
if (schedule==parentTarget.transform.childCount)
|
|
{
|
|
schedule = 0;
|
|
runState = 2;
|
|
isStart = true;
|
|
EndHiddenTargets?.ForEach(target => target.SetActive(false));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}*/
|
|
|
|
}
|
|
}
|