Search Unity

Assigning position to variable makes my game perform slower;

Discussion in 'Scripting' started by crowstormofficial, Jul 14, 2019.

  1. crowstormofficial

    crowstormofficial

    Joined:
    Jul 14, 2019
    Posts:
    5
    I created a GameObject that can travel in the main scene. I implemented a code restricting it's movement so it wont go outside of the boundaries. To make better readability i did:

    Code (CSharp):
    1. float posY = transform.position.y;
    and so on. But when I started the game whenever character reached the boundaries and would try to move simultanously on x and y axis it would get slowed down significantly because there was some "bumping" happening;

    But when I did it likle that:

    Code (CSharp):
    1.       if(transform.position.y < -4.38f) {
    2.             transform.position = new Vector3(transform.position.x, -4.38f, 0);
    3.         } else if (transform.position.y > 0) {
    4.             transform.position = new Vector3(transform.position.x, 0, 0);
    5.         }
    6.  
    7.         if(transform.position.x > 6.25f) {
    8.             transform.position = new Vector3(6.25f, transform.position.y, 0);
    9.         } else if (transform.position.x < -6.25f) {
    10.             transform.position = new Vector3(-6.25f, transform.position.y, 0);
    11.         }
    The performance seem to be good, there is no bumping and character is not slowed down when it's trying to go outside of the boundaries.

    So my question is:
    Is assigning position (and different things like that, I'm a beginner co can't think of anything more right now) really affects performance?
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,800
    Well yes, every thing you do does.

    But what you have here should be unnoticeable.
     
  3. crowstormofficial

    crowstormofficial

    Joined:
    Jul 14, 2019
    Posts:
    5
    I also thought that, but the difference was huge and I have no idea why.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    Assigning performance is not that intesive op, accessing external transform on the native side is.

    So if you want to assign position, and modify it a bunch of times, its better to do:
    Code (CSharp):
    1. Vector3 pos = transform.position;
    2.  
    3. if (pos ...){
    4.   pos.x = ....
    5. }
    6.  
    7. transform.position = pos;
    As a bonus, your code will look better.

    Caching transform on the managed side is another thing to do if you're interested in micro-optimizations. Just do it in the OnValidate like so:

    Code (CSharp):
    1. [SerializeField]
    2. private Transform _transform = default;
    3. protected virtual void OnValidate() {
    4.     _transform = transform;
    5. }
    It's not neccessary to do, but it can squeese a little bit of op time by removing expensive external fetch call to the native side. (Reference is deserialized by Unity only once instead of accessing external native side each time you do "transform")
     
    Last edited: Jul 15, 2019
    crowstormofficial likes this.
  5. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    364
    Setting position can actually be quite expensive depending on the hierarchy of parent objects, because all those parent transforms have to be accounted for.

    You're always better off using localPosition for anything with a high frequency.
     
    xVergilx likes this.
  6. crowstormofficial

    crowstormofficial

    Joined:
    Jul 14, 2019
    Posts:
    5
    Thank you, it definitely helps to learn that at the very beginning :)