Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CombatantSight : MonoBehaviour
- {
- public List<GameObject> potentialViewedObjects;
- public List<GameObject> viewedCombatants;
- public bool isDebug;
- int layerMask;
- public bool hasPlayerLock = false;
- // Start is called before the first frame update
- void Awake()
- {
- layerMask = 1 << 9;
- //layerMask = ~layerMask;
- }
- private void Update()
- {
- UpdateSight();
- if( isDebug )
- {
- foreach( GameObject viewed in viewedCombatants )
- {
- Vector3 target = viewed.transform.position;
- Vector3 direction = target - transform.position;
- Debug.DrawRay( transform.position, direction, Color.red );
- }
- }
- }
- public void UpdateSight()
- {
- foreach( GameObject potential in potentialViewedObjects )
- {
- RunSightCheck( potential );
- }
- }
- private void RunSightCheck( GameObject viewable )
- {
- Vector3 target = viewable.transform.position;
- RaycastHit hit;
- Vector3 direction = target - transform.position;
- Debug.DrawRay( transform.position, direction, Color.green );
- if( Physics.Linecast( transform.position, target, out hit, layerMask, QueryTriggerInteraction.Ignore ) )
- {
- if( hit.collider.gameObject != viewable )
- {
- //Debug.Log( "hit " + hit.collider.gameObject.name + " , " + viewable.name );
- Debug.DrawRay( transform.position, direction, Color.red );
- //Debug.Log( hit );
- if( viewedCombatants.Contains( viewable ) )
- {
- viewedCombatants.Remove( viewable );
- }
- if( viewable.tag == "Player" )
- {
- hasPlayerLock = false;
- }
- }
- else
- {
- //Debug.Log( "found viewable combatant" );
- Debug.DrawRay( transform.position, direction, Color.white );
- if( !viewedCombatants.Contains( viewable ) )
- {
- viewedCombatants.Add( viewable );
- }
- if (viewable.tag == "Player" )
- {
- hasPlayerLock = true;
- }
- }
- }
- }
- private void OnTriggerEnter( Collider other )
- {
- //Debug.Log( "Trigger enter" );
- if( other.gameObject.layer == 9 )
- {
- //Debug.Log( "Other Combatant in view, adding to potential" );
- potentialViewedObjects.Add( other.gameObject );
- RunSightCheck( other.gameObject );
- }
- //else if( other.gameObject.tag == "Obstacle" )
- //{
- //}
- }
- private void OnTriggerExit( Collider other )
- {
- if( other.gameObject.layer == 9 )
- {
- potentialViewedObjects.Remove( other.gameObject );
- if( viewedCombatants.Contains( other.gameObject ) )
- {
- viewedCombatants.Remove( other.gameObject );
- }
- if( other.gameObject.tag == "Player" )
- {
- hasPlayerLock = false;
- }
- }
- //else if( other.gameObject.tag == "Obstacle" )
- //{
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement