Advertisement
Pro_Unit

Maelstrom

Apr 28th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using GameCore;
  2.  
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. namespace QuestEngine
  7. {
  8.     public class Maelstrom : MonoBehaviour
  9.     {
  10.         [SerializeField] TrailRenderer trail;
  11.         [SerializeField] UnityEvent onCross;
  12.         private Vector3[] positions;
  13.         Vector3 center;
  14.         float angles;
  15.         [SerializeField] int maxCountPositions = 10;
  16.  
  17.         void Update ()
  18.         {
  19.             int positionCount = trail.positionCount;
  20.             if (positionCount == 0 || positionCount < maxCountPositions) return;
  21.             positions = new Vector3[positionCount];
  22.             trail.GetPositions (positions);
  23.  
  24.             for (int i = 0; i < maxCountPositions; i++)
  25.                 Debug.DrawRay (positions[i], Vector3.up * 10f, Color.red);
  26.  
  27.             center = FindCenter (positions);
  28.  
  29.             // Debug.LogFormat ("center = {0}", center);
  30.             // Debug.Log (positions.Log ());
  31.  
  32.             Debug.DrawRay (center, Vector3.up * 10f, Color.green);
  33.  
  34.             Vector3 lastPoint = positions[0];
  35.             for (int i = 1; i < maxCountPositions; i++)
  36.             {
  37.                 angles += Vector3.Angle (center - lastPoint, center - positions[i]);
  38.  
  39.                 if (angles >= 360f)
  40.                     onCross.Invoke ();
  41.  
  42.                 lastPoint = positions[i];
  43.             }
  44.             // Debug.LogFormat ("angles = {0}", angles);
  45.             angles = 0;
  46.         }
  47.         public Vector3 FindCenter (Vector3[] positions)
  48.         {
  49.             float minX, maxX, minY, maxY, minZ, maxZ;
  50.             minX = positions[0].x;
  51.             maxX = positions[0].x;
  52.             minY = positions[0].y;
  53.             maxY = positions[0].y;
  54.             minZ = positions[0].z;
  55.             maxZ = positions[0].z;
  56.  
  57.             foreach (Vector3 pos in positions)
  58.             {
  59.                 if (pos.x < minX)
  60.                     minX = pos.x;
  61.                 if (pos.x > maxX)
  62.                     maxX = pos.x;
  63.                 if (pos.y < minY)
  64.                     minY = pos.y;
  65.                 if (pos.y > maxY)
  66.                     maxY = pos.y;
  67.                 if (pos.z < minZ)
  68.                     minZ = pos.z;
  69.                 if (pos.z > maxZ)
  70.                     maxZ = pos.z;
  71.             }
  72.  
  73.             // Геометрический центр между всеми объектами.
  74.             Vector3 position = new Vector3 ((minX + maxX) / 2f, (minY + maxY) / 2f, (minZ + maxZ) / 2f);
  75.             return position;
  76.         }
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement