Advertisement
Flynny85

Check if an object can pass

May 25th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9.  
  10. public class test : MonoBehaviour {
  11.  
  12.     private Vector3 _directionForward;
  13.     private float _distance_ToTarget_01; private float _distance_ToTarget_02;
  14.     private float _distance_angle_01; private float _distance_angle_02;
  15.     public float _oppositeLength; public float _oppositeAngle;
  16.  
  17.     public float Check_ObjectIsInPath( Vector3 _position_Start , Vector3 _position_Ahead , Vector3 _position_Target , float _objectRadius){
  18.         _directionForward = _position_Start - _position_Ahead;
  19.         _distance_ToTarget_01 = MyMath.Get_XZ_DistanceAccurate(_position_Start , _position_Target);
  20.         _distance_ToTarget_02 = MyMath.Get_XZ_DistanceAccurate(_position_Ahead , _position_Target);
  21.         if(_distance_ToTarget_01 > _distance_ToTarget_02){
  22.             // facing position ..
  23.             _distance_angle_01 = Vector3.Angle (_position_Start - _position_Target, _directionForward);
  24.             // opposite side length we need == sin of angle01 * length of hyp ..
  25.             _oppositeAngle = Mathf.Sin( _distance_angle_01 * Mathf.Deg2Rad );
  26.             _oppositeLength = _oppositeAngle * _distance_ToTarget_01;
  27.         } else {
  28.             // position behind ..
  29.             _oppositeLength = _objectRadius * 2f; // will clear path regardless ..
  30.         }
  31.         return _oppositeLength; // returns intercept distance from current direction of object. if less than radious will collide ..
  32.     }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.     private Vector3 _vect_sp;
  40.     private Vector3 _vect_tp;
  41.     private Vector3 _vect_hit;
  42.  
  43.     public Transform _car1;
  44.     public Transform _car2;
  45.  
  46.     private float _distance_start_hit;
  47.     private float _distance_start_tp;
  48.     private float _distance_tp_hit;
  49.  
  50.     public float _radius = 1f;
  51.  
  52.     private float _angle;
  53.  
  54.     #if UNITY_EDITOR
  55.     private Vector3 _tempVect_debug;
  56.     private int _count_debug;
  57.  
  58.     private Color _debug_Colour01 = Color.cyan;
  59.     private Color _debug_Colour02 = Color.magenta;
  60.     public float _result;
  61.     public float _warningOffset = 0.3f;
  62.     void OnDrawGizmosSelected() {
  63.         if (EditorApplication.isPlaying) {
  64.  
  65.             if (Global_Vars._Global_GameState._current_gameState == Game_State.GameStates.StartScreen) {
  66.  
  67.                 _vect_sp = _car1.position;
  68.                 _vect_tp = _car1.position + (_car1.forward * 0.5f);
  69.                 _vect_hit = _car2.position;
  70.  
  71.                 _angle = Check_ObjectIsInPath( _vect_sp , _vect_tp , _vect_hit , _radius );
  72.                 _result = (_radius / _angle);
  73.  
  74.                 Gizmos.color = Color.Lerp (Color.white , Color.red , (_result + _warningOffset) );
  75.                 Gizmos.DrawWireCube (_car1.position, Vector3.one *  (_result + _warningOffset)  );
  76.                 _vect_tp = _vect_hit + (_car1.right * _oppositeLength);
  77.                 Gizmos.DrawLine ( _vect_sp , _vect_tp );
  78.                 Gizmos.DrawLine ( _vect_tp , _vect_hit );
  79.                 Gizmos.DrawLine ( _vect_sp , _vect_hit );
  80.             }
  81.         }
  82.     }
  83.     #endif
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement