Advertisement
_EagleOwle_

CarouselPrewievPlace

Sep 18th, 2022
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | Gaming | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class CarouselPrewievPlace : MonoBehaviour
  5. {
  6.     [SerializeField] private float speedRotation = 3;
  7.  
  8.     private PlacePoint[] points;
  9.     private PlacePoint currentPoint;
  10.  
  11.     private float nextAngle;
  12.     private float oldAngle;
  13.  
  14.     private int pointCount;
  15.  
  16.     private void Start()
  17.     {
  18.         pointCount = PrefabsStore.Instance.balls.Count;
  19.         points = new PlacePoint[pointCount];
  20.  
  21.         for (int i = 0; i < pointCount; i++)
  22.         {
  23.             UIPresentViewe tmp = Instantiate(PrefabsStore.Instance.balls[i].viewePrefab);
  24.             int angle = 360 / pointCount;
  25.             tmp.transform.position = RandomCircle(transform.position, 0.3f, angle * i);
  26.             tmp.transform.parent = transform;
  27.             tmp.transform.localScale = Vector3.one * 0.1f;
  28.             points[i].gameObject = tmp.gameObject;
  29.             points[i].angle = angle * i;
  30.         }
  31.  
  32.         SetCurrentPoint(nextAngle);
  33.     }
  34.  
  35.     public void GoNext()
  36.     {
  37.         StartRotation(nextAngle - (360 / pointCount));
  38.     }
  39.  
  40.     public void GoPrevious()
  41.     {
  42.         StartRotation(nextAngle + (360 / pointCount));
  43.     }
  44.  
  45.     private void SetCurrentPoint(float angle)
  46.     {
  47.         foreach (var item in points)
  48.         {
  49.             if(item.angle == angle)
  50.             {
  51.                 currentPoint = item;
  52.                 if (currentPoint.gameObject.TryGetComponent(out VisualEffect effect))
  53.                 {
  54.                     effect.ShowEffect();
  55.                 }
  56.                 return;
  57.             }
  58.         }
  59.  
  60.         Debug.LogError("No Point at angle " + angle);
  61.     }
  62.  
  63.     private void StartRotation(float angle)
  64.     {
  65.         oldAngle = nextAngle;
  66.  
  67.         if(currentPoint.gameObject.TryGetComponent(out VisualEffect effect))
  68.         {
  69.             effect.HideEffect();
  70.         }
  71.  
  72.         StopAllCoroutines();
  73.         StartCoroutine(Rotate(angle));
  74.         nextAngle = ClampAngle(angle);
  75.         SetCurrentPoint(nextAngle);
  76.     }
  77.  
  78.     private IEnumerator Rotate(float targetAngle)
  79.     {
  80.         while (transform.rotation.y != targetAngle)
  81.         {
  82.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), speedRotation * Time.deltaTime);
  83.  
  84.             yield return null;
  85.         }
  86.  
  87.         transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
  88.         yield return null;
  89.     }
  90.  
  91.     private Vector3 RandomCircle(Vector3 center, float radius, int ange)
  92.     {
  93.         float ang = ange;
  94.         Vector3 pos;
  95.         pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
  96.         pos.z = center.z - radius * Mathf.Cos(ang * Mathf.Deg2Rad);
  97.         pos.y = center.y;
  98.         return pos;
  99.     }
  100.  
  101.     private float ClampAngle(float angle)
  102.     {
  103.         if (angle >= 360f)
  104.         {
  105.             return angle - (360f * (int)(angle / 360f));
  106.         }
  107.         else if (angle >= 0f)
  108.         {
  109.             return angle;
  110.         }
  111.         else
  112.         {
  113.             float f = angle / -360f;
  114.             int i = (int)f;
  115.             if (f != i)
  116.                 ++i;
  117.             return angle + (360f * i);
  118.         }
  119.     }
  120.  
  121.     #region Debug
  122.     private void OnValidate()
  123.     {
  124.         pointCount = PrefabsStore.Instance.balls.Count;
  125.     }
  126.  
  127.     private void Update()
  128.     {
  129.         if (Input.GetKeyDown(KeyCode.A))
  130.         {
  131.             GoNext();
  132.         }
  133.  
  134.         if (Input.GetKeyDown(KeyCode.D))
  135.         {
  136.             GoPrevious();
  137.         }
  138.     }
  139.    
  140.     private void OnDrawGizmos()
  141.     {
  142.         for (int i = 0; i < pointCount; i++)
  143.         {
  144.             int angle = 360 / pointCount;
  145.             Vector3 startPosition = transform.position;
  146.             Vector3 endPosition = RandomCircle(startPosition, 0.3f, angle * i);
  147.             Gizmos.DrawLine(startPosition, endPosition);
  148.         }
  149.     }
  150.     #endregion
  151.  
  152.     private struct PlacePoint
  153.     {
  154.         public GameObject gameObject;
  155.         public int angle;
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement