Advertisement
Guest User

Untitled

a guest
Aug 11th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. namespace FIMSpace.FLook
  5. {
  6.     public class FLookAnimator_Demo_NPCPriority1 : MonoBehaviour
  7.     {
  8.         public string ImportantTargetTag = "Player";
  9.         public string NPCTag;
  10.  
  11.         private FLookAnimator npcLookAnimator;
  12.         private Transform importantTarget;
  13.         private Transform targetInRange;
  14.  
  15.         // When component is added inside editor, it reads object's tag to be putted in NPCTag variable
  16.         private void Reset()
  17.         {
  18.             NPCTag = gameObject.tag;
  19.         }
  20.  
  21.         private void Start()
  22.         {
  23.             npcLookAnimator = GetComponent<FLookAnimator>();
  24.         }
  25.  
  26.         // When someone steps in range
  27.         private void OnTriggerEnter(Collider other)
  28.         {
  29.             if (other.tag == ImportantTargetTag)
  30.             {
  31.                 importantTarget = other.transform;
  32.                 UpdateLookTarget();
  33.             }
  34.             else
  35.                 if (other.tag == NPCTag)
  36.             {
  37.                 targetInRange = other.transform;
  38.                 UpdateLookTarget();
  39.             }
  40.         }
  41.  
  42.  
  43.         // When someone left look at range
  44.         private void OnTriggerExit(Collider other)
  45.         {
  46.             if (other.tag == ImportantTargetTag)
  47.             {
  48.                 importantTarget = null;
  49.                 UpdateLookTarget();
  50.             }
  51.             else
  52.                 if (other.tag == NPCTag)
  53.                 {
  54.                     targetInRange = null;
  55.                     UpdateLookTarget();
  56.                 }
  57.         }
  58.  
  59.  
  60.         private void UpdateLookTarget()
  61.         {
  62.             // If important target is set, we looking at it (player)
  63.             if (importantTarget)
  64.                 npcLookAnimator.ObjectToFollow = importantTarget;
  65.             else
  66.                 if (targetInRange)
  67.                     npcLookAnimator.ObjectToFollow = targetInRange;
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement