Advertisement
FFaUniHan

Look.cs Optimization

Oct 27th, 2021 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. //You can simply copy and paste this entire script if you want to skip the tutorial
  2. //Just do a Ctrl + A on your Visual Studio, and paste (Ctrl + V)
  3.  
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7.  
  8. public class Look : MonoBehaviour
  9. {
  10.     public GameObject target;               //reference to the Target gameobject
  11.     public GameObject face;                 //reference to the character's face
  12.     public GameObject nearestObject;        //current nearest object
  13.     public float smoothTime = 1;            //the smoothing timing
  14.     public List<GameObject> objectsInSight;
  15.  
  16.     private Vector3 currentVelocity;
  17.  
  18.     private void Update()
  19.     {
  20.         MoveTargetLookAt();
  21.     }
  22.  
  23.     private void OnTriggerEnter(Collider other)
  24.     {
  25.         if (other.CompareTag("Wall")) return;
  26.  
  27.         if(nearestObject == null)
  28.         {
  29.             nearestObject = other.gameObject;
  30.         }
  31.  
  32.         if(!objectsInSight.Contains(other.gameObject))
  33.         {
  34.             objectsInSight.Add(other.gameObject);
  35.         }
  36.     }
  37.  
  38.     private void OnTriggerExit(Collider other)
  39.     {
  40.         if(objectsInSight.Contains(other.gameObject))
  41.         {
  42.             objectsInSight.Remove(other.gameObject);
  43.         }
  44.  
  45.         if(other.gameObject == nearestObject)
  46.         {
  47.             FindNearestObject();
  48.         }
  49.     }
  50.  
  51.     private void MoveTargetLookAt()
  52.     {
  53.         if(nearestObject != null)
  54.         {
  55.             target.transform.position = Vector3.SmoothDamp(target.transform.position,
  56.                 nearestObject.transform.position, ref currentVelocity, smoothTime * Time.deltaTime);
  57.         }
  58.         else
  59.         {
  60.             target.transform.position = Vector3.SmoothDamp(target.transform.position,
  61.                 face.transform.position + new Vector3(0, 1.5f, 1f), ref currentVelocity, smoothTime * Time.deltaTime);
  62.         }
  63.     }
  64.  
  65.     //Note: Original code from the tutorial.
  66.     //To quickly uncomment this code, select all lines in green and press Ctrl + K + U
  67.     //To comment it again, select all and press Ctrl + K + C
  68.     //private void FindNearestObject()
  69.     //{
  70.     //  if (objectsInSight.Count == 0)
  71.     //  {
  72.     //      nearestObject = null;
  73.     //  }
  74.     //  else if (objectsInSight.Count == 1)
  75.     //  {
  76.     //      nearestObject = objectsInSight[0];
  77.     //  }
  78.     //  else
  79.     //  {
  80.     //      nearestObject = objectsInSight[0];
  81.     //      for (int i = 1; i < objectsInSight.Count; i++)
  82.     //      {
  83.     //          if (Vector3.Distance(face.transform.position, objectsInSight[i].transform.position)
  84.     //              < Vector3.Distance(face.transform.position, nearestObject.transform.position))
  85.     //          {
  86.     //              nearestObject = objectsInSight[i];
  87.     //          }
  88.     //      }
  89.     //  }
  90.     //}
  91.  
  92.     //Optimized version
  93.     private void FindNearestObject()
  94.     {
  95.         if(objectsInSight.Count == 0)
  96.         {
  97.             nearestObject = null;
  98.         }
  99.         else if(objectsInSight.Count == 1)
  100.         {
  101.             nearestObject = objectsInSight[0];
  102.         }
  103.         else
  104.         {
  105.             //This is just a variable to hold our current nearest object (caching)
  106.             nearestObject = objectsInSight[0];
  107.             var nearestObjectDistance = getDistance(face.transform.position,
  108.                                             nearestObject.transform.position);
  109.  
  110.             for (int i = 1; i < objectsInSight.Count; i++)
  111.             {
  112.  
  113.                 //Optimized version: We don't need to recalculate distance between
  114.                 //the current nearest object and the character's face if the current nearest object
  115.                 //is still the same object
  116.  
  117.                 if (getDistance(face.transform.position, objectsInSight[i].transform.position)
  118.                     < nearestObjectDistance)
  119.                 {
  120.                     nearestObject = objectsInSight[i];
  121.  
  122.                     nearestObjectDistance = getDistance(face.transform.position,
  123.                                             nearestObject.transform.position);
  124.                 }
  125.             }
  126.         }  
  127.     }
  128.  
  129.     //Optimized version: We've replaced Vector3.Distance with sqrMagnitude.
  130.     //Apparently, Vector3.Distance uses square root function which is more costly.
  131.     //I don't quite understand that myself, so let's just follow the pros!
  132.     private float getDistance(Vector3 a, Vector3 b)
  133.     {
  134.         return (a - b).sqrMagnitude;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement