Kira Angelic
Kira Angelic

Reputation: 15

Avoid else if too much in fixedupdate

I'm new to Unity.
Due to multiple else if (around 40), its decrease fps of my game.
How to optimize too many else if condition like this script?

private void FixedUpdate()
{
    Vector3 desiredPosition = target.position + offset;
    Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    transform.position = smoothedPosition;
    if (PisauScript.nonaktifkamera.manjat == true)
    {
        offset.x = -5;
        offset.y = 14;
        offset.z = 6;
    }
    else if (PisauScript.nonaktifkamera.manjat2 == true)
    {
        offset.x = -14;
        offset.y = 25.5f;
        offset.z = 6;
    }
    else if (PisauScript.nonaktifkamera.manjat4 == true)
    {
        offset.x = -46;
    }
    else if (PisauScript.nonaktifkamera.manjat5 == true)
    {
        offset.x = -62.7f;
        offset.y = 17.5f;
    }
    //else if { }
    //...
    //else if { }
    

Upvotes: 1

Views: 164

Answers (3)

Fatih Aslan
Fatih Aslan

Reputation: 131

If you are using else if only and not if then you only have one bool which is true. So you can make a list of Vector3 (or floats this is your choice) and one int instead of bools. Something Like this:

List<Vector3> vectors=new List<Vector3>(){new Vector3(-5f,14,6)};
int selectedvector=0;
private void FixedUpdate()
{
    Vector3 desiredPosition = target.position + offset;
    Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    transform.position = smoothedPosition;
    offset=vectors[selectedvector];
}

Upvotes: 0

Frenchy
Frenchy

Reputation: 17037

One idea to avoid lot of if/else tests,

following your sample, you have only one (if/else if) true

so change your logic to use an index:

suppose you have:

PisauScript.nonaktifkamera.manjat = 0 for PisauScript.nonaktifkamera.manjat  = true
PisauScript.nonaktifkamera.manjat = 1 for PisauScript.nonaktifkamera.manjat1 = true
PisauScript.nonaktifkamera.manjat = 2 for PisauScript.nonaktifkamera.manjat2 = true

and so on.. my first idea was to use a dictionary, but you could use an array here

you create an array of vector3 :

public Vector3[] vector = new Vector3[50];

then you load all values (following your if/else if tests):

vector[0] = new Vector3(-5f, 14f, 6f));
vector[1] = new Vector3(-14f, 25.5f, 6f));
vector[2] = new Vector3(-14f, float.NegativeInfinity, float.NegativeInfinity));
  :
  :

i use float.NegativeInfinity to indicate that i dont want to change the value.

finally you just have to test one value in general method

private void FixedUpdate()
{
    :
    :
    var v = vector[PisauScript.nonaktifkamera.manjat];
    
    if(!float.IsNegativeInfinity(v.x)) offset.x = v.x;
    if(!float.IsNegativeInfinity(v.y)) offset.y = v.y;
    if(!float.IsNegativeInfinity(v.z)) offset.z = v.z;
}

I have no tested errors..

I recommend you to use the unity analyser... to see where you consume cpu

Because are you sure using lot of if/else if is really bad in performance?

Upvotes: 1

Prithvi Patel
Prithvi Patel

Reputation: 9

There is one blog that might be useful for you.

Link for blog: https://medium.com/@janalmazora/how-to-prevent-using-if-else-statements-in-your-code-7e05e43afde#:~:text=Avoid%20using%20nested%20if%2Delse,get%20this%20(Code%201.4).

Also, you can try Branchless Programming if you really want to avoid if-else statements. This will increase fps for sure.

Link for Branchless programming: https://www.youtube.com/watch?v=bVJ-mWWL7cE

Upvotes: 0

Related Questions