Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Small optimization dilemma

Discussion in 'Scripting' started by laur-ganta, Mar 10, 2015.

  1. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    Hello. I recently started being serious about using Unity and I am mad about optimizing every little line of code I write.

    Here is a short example and I can't decide which would be faster:

    I have a script for smoothly following a target around.

    Code (CSharp):
    1. void Update () {
    2.  
    3.         if (_state == State.Locked) {
    4.             FollowTarget ();
    5.         }
    6.                 /* some code */
    7. }
    8.  
    9. void FollowTarget()
    10.     {  // _distance is the desired distance between camera and target, x - Y axis, y - Z axis
    11.        _wantedPosition = new Vector3 (target.position.x, target.position.y + _distance.x,
    12.                                      target.position.z  -  _distance.y);
    13.         if (dampingEnabled) {    
    14.                 _wantedPosition.x = Mathf.Lerp (_myTransform.position.x, _wantedPosition.x,
    15.                                       damping * Time.deltaTime);
    16.                 _wantedPosition.y = Mathf.Lerp (_myTransform.position.y, _wantedPosition.y,
    17.                                       damping * Time.deltaTime);
    18.                 _wantedPosition.z = Mathf.Lerp (_myTransform.position.z, _wantedPosition.z,
    19.                                       damping * Time.deltaTime);
    20.                 _myTransform.position = _wantedPosition;
    21.         } else {
    22.             _myTransform.position = _wantedPosition;
    23.         }
    24.     }
    25.  
    This of course calculates the wanted position every frame.

    Would doing this be an optimization?

    Code (CSharp):
    1. void FollowTarget()
    2.     {
    3.        _wantedPosition = new Vector3 (target.position.x, target.position.y + _distance.x,
    4.                                    target.position.z  -  _distance.y);
    5.         if (dampingEnabled) {    
    6.             if(Vector3.Distance(_wantedPosition, _myTransform.position) > breakDistance )
    7.             {   // where breakDistance is a small float i.e. 0.01f
    8.                 _wantedPosition.x = Mathf.Lerp (_myTransform.position.x, _wantedPosition.x,
    9.                                                   damping * Time.deltaTime);
    10.                 _wantedPosition.y = Mathf.Lerp (_myTransform.position.y, _wantedPosition.y,
    11.                                                    damping * Time.deltaTime);
    12.                 _wantedPosition.z = Mathf.Lerp (_myTransform.position.z, _wantedPosition.z,
    13.                                                     damping * Time.deltaTime);
    14.                 _myTransform.position = _wantedPosition;
    15.              }
    16.         } else {
    17.             _myTransform.position = _wantedPosition;
    18.         }
    19.     }
    20.  


    I think the second alternative is better since it only calculates the distance once whilst Lerp is called 3 times, so when the camera is at the desired position the Lerp calls are avoided this way. I only ask because I wasn't sure which is faster, Lerp or Distance.
     
  2. Tiatang

    Tiatang

    Joined:
    Jun 1, 2014
    Posts:
    31
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Distance is more expensive than Lerp. It requires a square root calculation which, so far as math calculations go, is harder on the cpu.

    You could not compare it at all, or use Vector3 == Vector3, which returns true if the Vector3's are in the same spot or very close together, which looks like what you want.

    Why are you lerping each dimension separately? Vector3 has a Lerp function as well:

    Code (CSharp):
    1. void FollowTarget() {
    2.  
    3.      _wantedPosition = new Vector3 (target.position.x, target.position.y + _distance.x,  target.position.z  -  _distance.y);  
    4.    
    5.     if (dampening)
    6.         transform.position = Vector3.Lerp(transform.position, _wantedPosition, dampening * Time.deltaTime);
    7.     else
    8.         transform.position = _wantedPosition;
    9.    
    10. }
     
    landon912 likes this.
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Good luck never finishing anything.
     
    Timelog likes this.
  5. bigdaddy

    bigdaddy

    Joined:
    May 24, 2011
    Posts:
    153
  6. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    Thank you very much for answers!

    I was skeptical about asking this since I believed it was a trivial question, but I actualy learned a lot here.

    I changed to Vector3.Lerp() and I'll think again before starting to optimize code before I actualy need it.

    The == operator between the 2 Vector3 didn't work, the code in the if block would still be executed indefinitely.
    Before I moved on I made this function for checking if I'm close to destination, but now I don't know if Abs is expensive.

    Code (CSharp):
    1. private bool ReachedDestination(Vector3 v1, Vector3 v2, float minDistance)
    2.     {
    3.                 if (Mathf.Abs (v1.x - v2.x) < minDistance &&
    4.                     Mathf.Abs (v1.y - v2.y) < minDistance &&
    5.                     Mathf.Abs (v1.z - v2.z) < minDistance)
    6.                         return true;
    7.         return false;
    8.     }
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    You're waaaaay over-optimizing.

    Assigning the position of a transform every frame is not going to cost you any performance.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Dude, just finish the game. Then use the profiler to identify actual bottle necks. Fix them and forget the rest.
     
  9. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    Gonna do that, sir.
     
    Kiwasi likes this.