Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GameCore;
- using UnityEngine;
- using UnityEngine.Events;
- namespace QuestEngine
- {
- public class Maelstrom : MonoBehaviour
- {
- [SerializeField] TrailRenderer trail;
- [SerializeField] UnityEvent onCross;
- private Vector3[] positions;
- Vector3 center;
- float angles;
- [SerializeField] int maxCountPositions = 10;
- void Update ()
- {
- int positionCount = trail.positionCount;
- if (positionCount == 0 || positionCount < maxCountPositions) return;
- positions = new Vector3[positionCount];
- trail.GetPositions (positions);
- for (int i = 0; i < maxCountPositions; i++)
- Debug.DrawRay (positions[i], Vector3.up * 10f, Color.red);
- center = FindCenter (positions);
- // Debug.LogFormat ("center = {0}", center);
- // Debug.Log (positions.Log ());
- Debug.DrawRay (center, Vector3.up * 10f, Color.green);
- Vector3 lastPoint = positions[0];
- for (int i = 1; i < maxCountPositions; i++)
- {
- angles += Vector3.Angle (center - lastPoint, center - positions[i]);
- if (angles >= 360f)
- onCross.Invoke ();
- lastPoint = positions[i];
- }
- // Debug.LogFormat ("angles = {0}", angles);
- angles = 0;
- }
- public Vector3 FindCenter (Vector3[] positions)
- {
- float minX, maxX, minY, maxY, minZ, maxZ;
- minX = positions[0].x;
- maxX = positions[0].x;
- minY = positions[0].y;
- maxY = positions[0].y;
- minZ = positions[0].z;
- maxZ = positions[0].z;
- foreach (Vector3 pos in positions)
- {
- if (pos.x < minX)
- minX = pos.x;
- if (pos.x > maxX)
- maxX = pos.x;
- if (pos.y < minY)
- minY = pos.y;
- if (pos.y > maxY)
- maxY = pos.y;
- if (pos.z < minZ)
- minZ = pos.z;
- if (pos.z > maxZ)
- maxZ = pos.z;
- }
- // Геометрический центр между всеми объектами.
- Vector3 position = new Vector3 ((minX + maxX) / 2f, (minY + maxY) / 2f, (minZ + maxZ) / 2f);
- return position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement