EgonMilanVotrubec

ClosestCrouchDistance

Jul 25th, 2019
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1.     public class NewBehaviourScript : MonoBehaviour
  2.     {
  3.         // Drop all your crouch points in here, from Inspector.
  4.         public List<Transform> crouchPoints = new List<Transform> ( );
  5.  
  6.         public int CurrentCrouchPoint { get; set; } = -1;
  7.  
  8.         private Transform FindClosestCrouchPoint ()
  9.         {
  10.             var closestDistance = float.MaxValue;
  11.             Vector3 origin;
  12.             if ( CurrentCrouchPoint == -1 )
  13.                 origin = this.transform.localPosition;
  14.             else
  15.                 origin = crouchPoints [ CurrentCrouchPoint ].localPosition;
  16.  
  17.             var closestCrouchPoint = CurrentCrouchPoint;
  18.             for (int i = 0; i < crouchPoints.Count; i++ )
  19.             {
  20.                 if ( i == CurrentCrouchPoint ) continue;
  21.                 var tempDistance = Vector3.Distance ( origin, crouchPoints [ i ].localPosition );
  22.                 if ( tempDistance < closestDistance )
  23.                 {
  24.                     closestDistance = tempDistance;
  25.                     closestCrouchPoint = i;
  26.                 }
  27.             }
  28.  
  29.             CurrentCrouchPoint = closestCrouchPoint;
  30.             return crouchPoints [ CurrentCrouchPoint ];
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment