using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Animations; public class ActionDoor : MonoBehaviour { /// /// 애니메이션 실행 여부 /// public bool Run; /// /// 애니메이션 속도 /// public float Speed; /// /// 애니메이션 목표 회전값(Euler) 또는 목표 좌표 /// public Vector3 TargetRot; /// /// 이동 애니메이션 여부 /// true면 이동 false면 회전 애니메이션 /// public bool isMove; private Quaternion targetRot; private Quaternion initRot; private Vector3 targetPos; private Vector3 initPos; private float t; /// /// 애니메이션 종료 여부 /// private bool isEnd; /// /// 애니메이션 종료를 시작값으로(VR에서만) /// public bool VREndStart; void Start() { if (!isMove) { targetRot = Quaternion.Euler(TargetRot); initRot = transform.localRotation; } else { targetPos = TargetRot; initPos = transform.localPosition; } Initialize(); } void Initialize() { if (VREndStart && SceneTypeManager.Inst.IsVRMode) { EndInit(); } else { Init(); } } void EndInit() { if (!isMove) { transform.localRotation = targetRot; } else { transform.localPosition = targetPos; } isEnd = true; } void Init() { if (!isMove) { transform.localRotation = initRot; } else { transform.localPosition = initPos; } isEnd = false; } public void Play() { if (Run) { isEnd = !isEnd; t = 0; } Run = true; } void InitValues(bool end) { Run = false; isEnd = end; t = 0; } void Update() { if (Run) { t += Speed * Time.deltaTime; if (!isMove) { if (isEnd) { // 초기 회전값까지 애니메이션 transform.localRotation = Quaternion.Slerp(transform.localRotation, initRot, t); if (transform.localRotation == initRot) { InitValues(false); } } else { // 목표 회전값까지 애니메이션 transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRot, t); if (transform.localRotation == targetRot) { InitValues(true); } } } else { if (isEnd) { // 초기 좌표까지 애니메이션 transform.localPosition = Vector3.Lerp(transform.localPosition, initPos, t); if (transform.localPosition == initPos) { InitValues(false); } } else { // 목표 좌표까지 애니메이션 transform.localPosition = Vector3.Lerp(transform.localPosition, targetPos, t); if (transform.localPosition == targetPos) { InitValues(true); } } } } } }