Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- public class CamMoving : MonoBehaviour
- {
- [SerializeField] Rigidbody camRigidbody;
- [SerializeField] Camera camera;
- [SerializeField] float camMoveSpeed;
- [SerializeField] float minCamZoom;
- [SerializeField] float maxCamZoom;
- bool isMovingBlocked = false;
- private void Awake()
- {
- #if !UNITY_EDITOR
- camMoveSpeed *= 3;
- #endif
- }
- public void UpdatePos(float velocityX, float velocityZ)
- {
- if (!isMovingBlocked)
- camRigidbody.velocity = new Vector3(velocityX * camMoveSpeed, camRigidbody.position.y, velocityZ * camMoveSpeed);
- }
- public void UpdateZoom(float scale)
- {
- if (!isMovingBlocked)
- camera.fieldOfView = Mathf.Clamp(camera.fieldOfView * scale, minCamZoom, maxCamZoom);
- }
- /// <summary>
- /// Блокировать взаимодействие с камерой свайпами
- /// </summary>
- void BlockMoving(bool isBlock)
- {
- isMovingBlocked = isBlock;
- camRigidbody.velocity = Vector3.zero;
- }
- public void FocusOnObject(Transform target)
- {
- BlockMoving(true);
- //Преобразовываем центр экрана в мировые координаты
- var screenWorldPosCenter = camera.ViewportToWorldPoint(CamMoveController.CAM_CENTER_VIEWPOINT);
- //находим нужную позицию для камеры, чтоб позиция соответствовала состоянию, когда обьект в центре экрана
- var centeredObject = transform.position - (screenWorldPosCenter - target.position);
- centeredObject.y = transform.position.y;
- camRigidbody.transform.DOMove(centeredObject, 1).OnComplete(() => BlockMoving(false));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement