| | 1 | | using System.Collections; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | |
|
| | 5 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 6 | | { |
| | 7 | | public class PreviewCameraRotation : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler |
| | 8 | | { |
| | 9 | | public event System.Action<float> OnHorizontalRotation; |
| | 10 | |
|
| 50 | 11 | | public float rotationFactor = -15f; |
| | 12 | |
|
| 50 | 13 | | public float slowDownTime = 0.5f; |
| | 14 | |
|
| | 15 | | private float currentHorizontalRotationVelocity = 0f; |
| | 16 | |
|
| | 17 | | private float slowDownVelocity; |
| | 18 | |
|
| | 19 | | private Coroutine slowDownCoroutine; |
| | 20 | |
|
| | 21 | | private float timer; |
| | 22 | |
|
| 0 | 23 | | public void OnBeginDrag(PointerEventData eventData) { AudioScriptableObjects.buttonClick.Play(true); } |
| | 24 | |
|
| | 25 | | public void OnDrag(PointerEventData eventData) |
| | 26 | | { |
| 0 | 27 | | if (slowDownCoroutine != null) |
| | 28 | | { |
| 0 | 29 | | StopCoroutine(slowDownCoroutine); |
| 0 | 30 | | slowDownCoroutine = null; |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | currentHorizontalRotationVelocity = rotationFactor * eventData.delta.x; |
| 0 | 34 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public void OnEndDrag(PointerEventData eventData) |
| | 38 | | { |
| 0 | 39 | | timer = slowDownTime; |
| 0 | 40 | | slowDownVelocity = currentHorizontalRotationVelocity; |
| | 41 | |
|
| 0 | 42 | | if (slowDownCoroutine == null) |
| | 43 | | { |
| 0 | 44 | | slowDownCoroutine = StartCoroutine(SlowDown()); |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | private IEnumerator SlowDown() |
| | 51 | | { |
| 0 | 52 | | float inverseTimer = 1f / slowDownTime; |
| | 53 | |
|
| 0 | 54 | | while (timer > 0) |
| | 55 | | { |
| 0 | 56 | | timer -= Time.deltaTime; |
| 0 | 57 | | currentHorizontalRotationVelocity = Mathf.Lerp(slowDownVelocity, 0, 1 - (timer * inverseTimer)); |
| 0 | 58 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| | 59 | |
|
| 0 | 60 | | yield return null; |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | | } |
| | 64 | | } |