Answer the question
In order to leave comments, you need to log in
Code optimization?
There are 2 rather large scripts.
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);
}
}
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;
}
}
Answer the question
In order to leave comments, you need to log in
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();
}
if (!isGround)
return;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question