using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class WishPageCamera : MonoBehaviour { public GameObject wishPageGroup; private GameObject [] wishPages; private float mouseSpeedX; private float mouseSpeedY; public float cameraSpeed; private Vector2 targetPos; public bool isFreeze = false; // Start is called before the first frame update void Start() { var wishPageList = wishPageGroup.GetComponentsInChildren(); wishPages = new GameObject[wishPageList.Length]; for(int i = 0; i < wishPageList.Length; ++i) { wishPages[i] = wishPageList[i].gameObject; } } public void SetCameraFreeze(bool _isFreeze) { isFreeze = _isFreeze; } void CheckMouseMovement() { if (Input.GetMouseButton(0)) { mouseSpeedX = Input.GetAxis("Mouse X"); if (Mathf.Abs(mouseSpeedX) < float.Epsilon) return; targetPos = transform.position - new Vector3(mouseSpeedX * cameraSpeed * Time.fixedDeltaTime, 0.0f, 0.0f); transform.position = Vector3.Slerp(transform.position, targetPos, cameraSpeed * Time.fixedDeltaTime); } } void CheckNearest() { if (Input.GetMouseButtonUp(0)) { float nearestDistance = float.MaxValue; foreach(var wishPage in wishPages) { if (Mathf.Abs(wishPage.transform.position.x - transform.position.x) < nearestDistance) { nearestDistance = Mathf.Abs(wishPage.transform.position.x - transform.position.x); targetPos = wishPage.transform.position; } } transform.DOMove(targetPos, 0.3f); } } // Update is called once per frame void Update() { if (isFreeze) return; CheckMouseMovement(); CheckNearest(); //transform.position = Vector3.Lerp(transform.position, targetPos, cameraSpeed * Time.fixedDeltaTime); } }