P
P
piffo2020-05-02 00:00:46
C++ / C#
piffo, 2020-05-02 00:00:46

Code optimization?

There are 2 rather large scripts.

Here is the first
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
    Rigidbody2D rb;
    bool isGround = false;
    public float maxSpeed = 10f;
    private bool isFacingRight = true;
    private Animator anim;
    public BoxCollider2D[] col9;
    public CapsuleCollider2D col3;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        col9 = GetComponents<BoxCollider2D>();
        col3 = GetComponent<CapsuleCollider2D>();

        kuk();
    }

    void OnTriggerStay2D(Collider2D col)
    {
        if (col.tag == "ground") isGround = true;
    }
    void OnTriggerExit2D(Collider2D col)
    {
        if (col.tag == "ground") isGround = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGround)
        {
            puk();
            anim.SetBool("jump", true);
        }
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(Input.GetAxis("Horizontal") * 6f, rb.velocity.y);
        float move = Input.GetAxis("Horizontal");
        anim.SetFloat("Speed", Mathf.Abs(move));
        if (move > 0 && !isFacingRight)
            Flip();
        else if (move < 0 && isFacingRight)
            Flip();
        rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
        anim.SetBool("jump", isGround);
        if (!isGround)
            return;
    }

    void puk()
    {
        rb.AddForce(transform.up * 10f, ForceMode2D.Impulse);
    }

    void kuk()
    {
        col9[0].offset = new Vector2(0f, -0.41f);
        col9[1].offset = new Vector2(0f, -0.08f);
        col3.offset = new Vector2(0f, 0.29f);
    }

    private void Flip()
    {
        isFacingRight = !isFacingRight;
        transform.Rotate(0, 180, 0);
    }
}

This is the main script of the character, it hangs on the character prefab that is on every scene. These are the methods that should be on the character, on every scene. If you need to add something for a certain scene, then I create a separate script and already add it to the corresponding scene. Here's about each method:
Start, here we cache everything.
OnTriggerStay2D and OnTriggerExit2D to check if the character is on the ground or not.
Update for the jump.
Fixed Update to move right, left and flip it; also to prevent the character from jumping endlessly.
puk this I think is clear.
kuk so that the character's colliders move when the game starts.
Flip is also clear.

Here is the second script. It hangs on the character on one of the levels.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class transition : MonoBehaviour
{
    public GameObject came;
    public GameObject charter;
    public GameObject txt;
    public GameObject move1;
    public GameObject[] cames;
    int wer = 5;
    bool bol;
    Vector2 vec;
    Vector3 came1;
    Vector3 came2;

    private void Start()
    {
        StartCoroutine(fuk());
        bol = cames[0].GetComponent<camemove31>().bol;
        came1 = new Vector3(0f, 0f, 0f);
        came2 = new Vector3(3.93f, 0f, 0f);
    }

    private void Update()
    {
        Debug.Log(wer);
        if (bol == true)
        {
            cames[2].SetActive(false);
            cames[3].SetActive(true);
        }
        if (bol == false)
        {
            cames[2].SetActive(true);
            cames[3].SetActive(false);
        }
        if (came.transform.position == came1)
            bol = true;
        if (came.transform.position == came2)
            bol = false;
    }

    IEnumerator fuk()
    {
        txt.SetActive(true);
        yield return new WaitForSeconds(5f);
        txt.SetActive(false);
    }

    IEnumerator dick()
    {
        this.gameObject.GetComponent<Animator>().enabled = false;
        yield return new WaitForSeconds(0.1f);
        this.gameObject.GetComponent<Animator>().enabled = true;
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.name == "set5")
        {
            kok();
            came.transform.position = vec;

            if (wer == 4)
            {
                wer--;
                return;
            }
            if (wer == 3)
            {
                wer--;
                return;
            }
            if (wer != 4 || wer != 3)
                wer = 5;
        }
        if (col.gameObject.name == "wall3")
        {
            StartCoroutine(dick());
            charter.transform.position = new Vector3(12.456f, -2.817f, 90f);
            came.transform.position = came2;

            if (wer == 5)
            {
                wer--;
                return;
            }
            if (wer == 1)
                SceneManager.LoadScene("Level4");
            if (wer != 5 || wer != 1)
                wer = 5;
        }
        if (col.gameObject.name == "wall4")
        {
            StartCoroutine(dick());
            charter.transform.position = new Vector3(-8.527035f, -2.821f, 90f);
            came.transform.position = came1;

            if (wer == 2)
            {
                wer--;
                return;
            }
            if (wer != 2)
                wer = 5;
        }
        if (col.gameObject.name == "cametrig")
        {
            cames[0].SetActive(true);
        }
        if (col.gameObject.name == "cametrig2")
        {
            cames[1].SetActive(true);
        }
    }

    void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.name == "set5")
        {
            kok();
            came.transform.position = vec;
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject.name == "set5")
        {
            charter.transform.position = new Vector3(-7.62f, 2.79f, 90f);
            move1.SetActive(true);
        }
    }

    void kok()
    {
        vec = charter.transform.position;
    }
}

Yes, I know a terrible shitty code, here I think nothing needs to be explained, just read. The general concept of the level is as follows: there is no exit from the level, if the character goes to the left, then he will be transferred to the right, if to the left, then accordingly to the right. There is also a hole, if you jump there, the character will be transferred back to the ground. To pass the level you need to go left, down twice, right and left again.
So to the point. How can these scripts be optimized?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2CHEVSKII, 2020-05-02
@piffo

puk999 , okay, then I'll analyze your code:
In terms of readability, it's disgusting. Names of methods, variables, etc. should be self-descriptive, i.e. - the name should give an approximate idea of ​​the functionality.
These vectors are insane - it's better to put them into separate variables with understandable names, and reuse them. This will have a positive effect on performance as well.
Also - I usually put all the functionality called by hooks into separate methods, which is also useful for code readability. When you start sculpting components for 2k lines, you will understand that this is important.
FixedUpdate 1st script:
Your if-elseif can be turned into just one if, so as not to repeat the code:

if (move > 0 && !isFacingRight || move < 0 && isFacingRight)
{
    Flip();
}

Why the hell is this needed, I don’t rummage at all:
if (!isGround)
                return;

This method is not called anywhere else, why the hell does it exist? Then transfer everything from Update to a separate method, and put the puk() logic in the same place.
5eaca69b88e0b593471086.png
The same claim to the kok() method in the second script.
Well, in general, the code looks very diverse, this should not be, stick to one style.
Also - do not abuse GetComponents, they are not free, in terms of performance, it is better to pull everything that you will use into the fields in Start / Awake, and already use them.
Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question