Advertisement
blobic123

Test Script

May 12th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour {
  4.  
  5.     void Start () {
  6.         Vector3[] coords = {new Vector3(2,1,0), new Vector3(1,1,0), new Vector3(1,2,0), new Vector3(0,0,0)};
  7.         coords[3] = Find4th(coords[0], coords[1], coords[2]);
  8.         foreach (Vector3 v in coords) {
  9.             new GameObject().transform.position = v;
  10.         }
  11.     }
  12.  
  13.     public Vector3 Find4th(Vector3 a, Vector3 b, Vector3 c) {
  14.  
  15.         Vector3 point, cornerA, cornerB;
  16.  
  17.         Vector3[] points = new Vector3[] {a, b, c};
  18.         float angleDelta = 180;
  19.         int pointIndex = -1;
  20.  
  21.         //find point with angle closest to 90 degrees
  22.         for (int i = 0; i < 3; ++i) {
  23.             point = points[i];
  24.  
  25.             cornerA = points[i != 0 ? i-1 : 2];
  26.             cornerB = points[i != 2 ? i+1 : 0];
  27.  
  28.             Vector3 lineA = cornerA - point;
  29.             Vector3 lineB = cornerB - point;
  30.  
  31.             float ang = Mathf.Abs(Vector3.Angle(lineA, lineB)-90);
  32.             int ai = (i != 0) ? i-1 : 2;
  33.             int bi = (i != 2) ? i+1 : 0;
  34.             Debug.Log($"{i}: {ang}, {ai}, {bi}");
  35.  
  36.             if (ang < angleDelta) {
  37.                 angleDelta = ang;
  38.                 pointIndex = i;
  39.             }
  40.         }
  41.  
  42.         point = points[pointIndex];
  43.         cornerA = points[pointIndex != 0 ? pointIndex-1 : 2];
  44.         cornerB = points[pointIndex != 2 ? pointIndex+1 : 0];
  45.  
  46.         Debug.Log(cornerA + cornerB - point);
  47.  
  48.         return cornerA + cornerB - point;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement