Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CombatantSight : MonoBehaviour
  6. {
  7.     public List<GameObject> potentialViewedObjects;
  8.     public List<GameObject> viewedCombatants;
  9.     public bool isDebug;
  10.     int layerMask;
  11.     public bool hasPlayerLock = false;
  12.     // Start is called before the first frame update
  13.     void Awake()
  14.     {
  15.         layerMask = 1 << 9;
  16.         //layerMask = ~layerMask;
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         UpdateSight();
  22.         if( isDebug )
  23.         {
  24.             foreach( GameObject viewed in viewedCombatants )
  25.             {
  26.                 Vector3 target = viewed.transform.position;
  27.                 Vector3 direction = target - transform.position;
  28.                 Debug.DrawRay( transform.position, direction, Color.red );
  29.             }
  30.         }
  31.  
  32.     }
  33.  
  34.  
  35.     public void UpdateSight()
  36.     {
  37.         foreach( GameObject potential in potentialViewedObjects )
  38.         {
  39.             RunSightCheck( potential );
  40.         }
  41.     }
  42.  
  43.     private void RunSightCheck( GameObject viewable )
  44.     {
  45.         Vector3 target = viewable.transform.position;
  46.         RaycastHit hit;
  47.         Vector3 direction = target - transform.position;
  48.         Debug.DrawRay( transform.position, direction, Color.green );
  49.         if( Physics.Linecast( transform.position, target, out hit, layerMask, QueryTriggerInteraction.Ignore ) )
  50.         {
  51.             if( hit.collider.gameObject != viewable )
  52.             {
  53.                 //Debug.Log( "hit " + hit.collider.gameObject.name + " , " + viewable.name );
  54.                 Debug.DrawRay( transform.position, direction, Color.red );
  55.                 //Debug.Log( hit );
  56.                 if( viewedCombatants.Contains( viewable ) )
  57.                 {
  58.                     viewedCombatants.Remove( viewable );
  59.                 }
  60.                 if( viewable.tag == "Player" )
  61.                 {
  62.                     hasPlayerLock = false;
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 //Debug.Log( "found viewable combatant" );
  68.                 Debug.DrawRay( transform.position, direction, Color.white );
  69.                 if( !viewedCombatants.Contains( viewable ) )
  70.                 {
  71.                     viewedCombatants.Add( viewable );
  72.                 }
  73.                 if (viewable.tag == "Player" )
  74.                 {
  75.                     hasPlayerLock = true;
  76.                 }
  77.             }
  78.  
  79.         }
  80.  
  81.  
  82.     }
  83.  
  84.     private void OnTriggerEnter( Collider other )
  85.     {
  86.         //Debug.Log( "Trigger enter" );
  87.         if( other.gameObject.layer == 9 )
  88.         {
  89.             //Debug.Log( "Other Combatant in view, adding to potential" );
  90.             potentialViewedObjects.Add( other.gameObject );
  91.             RunSightCheck( other.gameObject );
  92.  
  93.         }
  94.        
  95.         //else if( other.gameObject.tag == "Obstacle" )
  96.         //{
  97.  
  98.         //}
  99.     }
  100.  
  101.  
  102.     private void OnTriggerExit( Collider other )
  103.     {
  104.         if( other.gameObject.layer == 9 )
  105.         {
  106.             potentialViewedObjects.Remove( other.gameObject );
  107.             if( viewedCombatants.Contains( other.gameObject ) )
  108.             {
  109.                 viewedCombatants.Remove( other.gameObject );
  110.             }
  111.             if( other.gameObject.tag == "Player" )
  112.             {
  113.                 hasPlayerLock = false;
  114.             }
  115.         }
  116.         //else if( other.gameObject.tag == "Obstacle" )
  117.         //{
  118.  
  119.         //}
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement